WordPress 4.0
[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         public 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                         /** This filter is documented in wp-includes/class.wp-styles.php */
97                         $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 );
98
99                         if ( $obj->extra['rtl'] === 'replace' ) {
100                                 $tag = $rtl_tag;
101                         } else {
102                                 $tag .= $rtl_tag;
103                         }
104                 }
105
106                 if ( isset($obj->extra['conditional']) && $obj->extra['conditional'] ) {
107                         $tag = "<!--[if {$obj->extra['conditional']}]>\n" . $tag . "<![endif]-->\n";
108                 }
109
110                 if ( $this->do_concat ) {
111                         $this->print_html .= $tag;
112                         if ( $inline_style = $this->print_inline_style( $handle, false ) )
113                                 $this->print_html .= sprintf( "<style type='text/css'>\n%s\n</style>\n", $inline_style );
114                 } else {
115                         echo $tag;
116                         $this->print_inline_style( $handle );
117                 }
118
119                 return true;
120         }
121
122         public function add_inline_style( $handle, $code ) {
123                 if ( !$code )
124                         return false;
125
126                 $after = $this->get_data( $handle, 'after' );
127                 if ( !$after )
128                         $after = array();
129
130                 $after[] = $code;
131
132                 return $this->add_data( $handle, 'after', $after );
133         }
134
135         public function print_inline_style( $handle, $echo = true ) {
136                 $output = $this->get_data( $handle, 'after' );
137
138                 if ( empty( $output ) )
139                         return false;
140
141                 $output = implode( "\n", $output );
142
143                 if ( !$echo )
144                         return $output;
145
146                 echo "<style type='text/css'>\n";
147                 echo "$output\n";
148                 echo "</style>\n";
149
150                 return true;
151         }
152
153         public function all_deps( $handles, $recursion = false, $group = false ) {
154                 $r = parent::all_deps( $handles, $recursion );
155                 if ( !$recursion ) {
156                         /**
157                          * Filter the array of enqueued styles before processing for output.
158                          *
159                          * @since 2.6.0
160                          *
161                          * @param array $to_do The list of enqueued styles about to be processed.
162                          */
163                         $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
164                 }
165                 return $r;
166         }
167
168         public function _css_href( $src, $ver, $handle ) {
169                 if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
170                         $src = $this->base_url . $src;
171                 }
172
173                 if ( !empty($ver) )
174                         $src = add_query_arg('ver', $ver, $src);
175
176                 /**
177                  * Filter an enqueued style's fully-qualified URL.
178                  *
179                  * @since 2.6.0
180                  *
181                  * @param string $src    The source URL of the enqueued style.
182                  * @param string $handle The style's registered handle.
183                  */
184                 $src = apply_filters( 'style_loader_src', $src, $handle );
185                 return esc_url( $src );
186         }
187
188         public function in_default_dir($src) {
189                 if ( ! $this->default_dirs )
190                         return true;
191
192                 foreach ( (array) $this->default_dirs as $test ) {
193                         if ( 0 === strpos($src, $test) )
194                                 return true;
195                 }
196                 return false;
197         }
198
199         public function do_footer_items() { // HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
200                 $this->do_items(false, 1);
201                 return $this->done;
202         }
203
204         public function reset() {
205                 $this->do_concat = false;
206                 $this->concat = '';
207                 $this->concat_version = '';
208                 $this->print_html = '';
209         }
210 }