]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class.wp-styles.php
WordPress 3.8
[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                 do_action_ref_array( 'wp_default_styles', array(&$this) );
33         }
34
35         function do_item( $handle ) {
36                 if ( !parent::do_item($handle) )
37                         return false;
38
39                 $obj = $this->registered[$handle];
40                 if ( null === $obj->ver )
41                         $ver = '';
42                 else
43                         $ver = $obj->ver ? $obj->ver : $this->default_version;
44
45                 if ( isset($this->args[$handle]) )
46                         $ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
47
48                 if ( $this->do_concat ) {
49                         if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) {
50                                 $this->concat .= "$handle,";
51                                 $this->concat_version .= "$handle$ver";
52
53                                 $this->print_code .= $this->print_inline_style( $handle, false );
54
55                                 return true;
56                         }
57                 }
58
59                 if ( isset($obj->args) )
60                         $media = esc_attr( $obj->args );
61                 else
62                         $media = 'all';
63
64                 $href = $this->_css_href( $obj->src, $ver, $handle );
65                 $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
66                 $title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
67
68                 $tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle );
69                 if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) {
70                         if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
71                                 $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
72                                 $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" ));
73                         } else {
74                                 $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
75                         }
76
77                         $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 );
78
79                         if ( $obj->extra['rtl'] === 'replace' ) {
80                                 $tag = $rtl_tag;
81                         } else {
82                                 $tag .= $rtl_tag;
83                         }
84                 }
85
86                 if ( isset($obj->extra['conditional']) && $obj->extra['conditional'] ) {
87                         $tag = "<!--[if {$obj->extra['conditional']}]>\n" . $tag . "<![endif]-->\n";
88                 }
89
90                 if ( $this->do_concat ) {
91                         $this->print_html .= $tag;
92                         if ( $inline_style = $this->print_inline_style( $handle, false ) )
93                                 $this->print_html .= sprintf( "<style type='text/css'>\n%s\n</style>\n", $inline_style );
94                 } else {
95                         echo $tag;
96                         $this->print_inline_style( $handle );
97                 }
98
99                 return true;
100         }
101
102         function add_inline_style( $handle, $code ) {
103                 if ( !$code )
104                         return false;
105
106                 $after = $this->get_data( $handle, 'after' );
107                 if ( !$after )
108                         $after = array();
109
110                 $after[] = $code;
111
112                 return $this->add_data( $handle, 'after', $after );
113         }
114
115         function print_inline_style( $handle, $echo = true ) {
116                 $output = $this->get_data( $handle, 'after' );
117
118                 if ( empty( $output ) )
119                         return false;
120
121                 $output = implode( "\n", $output );
122
123                 if ( !$echo )
124                         return $output;
125
126                 echo "<style type='text/css'>\n";
127                 echo "$output\n";
128                 echo "</style>\n";
129
130                 return true;
131         }
132
133         function all_deps( $handles, $recursion = false, $group = false ) {
134                 $r = parent::all_deps( $handles, $recursion );
135                 if ( !$recursion )
136                         $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
137                 return $r;
138         }
139
140         function _css_href( $src, $ver, $handle ) {
141                 if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
142                         $src = $this->base_url . $src;
143                 }
144
145                 if ( !empty($ver) )
146                         $src = add_query_arg('ver', $ver, $src);
147                 $src = apply_filters( 'style_loader_src', $src, $handle );
148                 return esc_url( $src );
149         }
150
151         function in_default_dir($src) {
152                 if ( ! $this->default_dirs )
153                         return true;
154
155                 foreach ( (array) $this->default_dirs as $test ) {
156                         if ( 0 === strpos($src, $test) )
157                                 return true;
158                 }
159                 return false;
160         }
161
162         function do_footer_items() { // HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
163                 $this->do_items(false, 1);
164                 return $this->done;
165         }
166
167         function reset() {
168                 $this->do_concat = false;
169                 $this->concat = '';
170                 $this->concat_version = '';
171                 $this->print_html = '';
172         }
173 }