]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/functions.wp-styles.php
WordPress 4.4
[autoinstalls/wordpress.git] / wp-includes / functions.wp-styles.php
1 <?php
2 /**
3  * BackPress Styles Procedural API
4  *
5  * @since 2.6.0
6  *
7  * @package WordPress
8  * @subpackage BackPress
9  */
10
11 /**
12  * Initialize $wp_styles if it has not been set.
13  *
14  * @global WP_Styles $wp_styles
15  *
16  * @since 4.2.0
17  *
18  * @return WP_Styles WP_Styles instance.
19  */
20 function wp_styles() {
21         global $wp_styles;
22         if ( ! ( $wp_styles instanceof WP_Styles ) ) {
23                 $wp_styles = new WP_Styles();
24         }
25         return $wp_styles;
26 }
27
28 /**
29  * Display styles that are in the $handles queue.
30  *
31  * Passing an empty array to $handles prints the queue,
32  * passing an array with one string prints that style,
33  * and passing an array of strings prints those styles.
34  *
35  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
36  *
37  * @since 2.6.0
38  *
39  * @param string|bool|array $handles Styles to be printed. Default 'false'.
40  * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
41  */
42 function wp_print_styles( $handles = false ) {
43         if ( '' === $handles ) { // for wp_head
44                 $handles = false;
45         }
46         /**
47          * Fires before styles in the $handles queue are printed.
48          *
49          * @since 2.6.0
50          */
51         if ( ! $handles ) {
52                 do_action( 'wp_print_styles' );
53         }
54
55         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
56
57         global $wp_styles;
58         if ( ! ( $wp_styles instanceof WP_Styles ) ) {
59                 if ( ! $handles ) {
60                         return array(); // No need to instantiate if nothing is there.
61                 }
62         }
63
64         return wp_styles()->do_items( $handles );
65 }
66
67 /**
68  * Add extra CSS styles to a registered stylesheet.
69  *
70  * Styles will only be added if the stylesheet in already in the queue.
71  * Accepts a string $data containing the CSS. If two or more CSS code blocks
72  * are added to the same stylesheet $handle, they will be printed in the order
73  * they were added, i.e. the latter added styles can redeclare the previous.
74  *
75  * @see WP_Styles::add_inline_style()
76  *
77  * @since 3.3.0
78  *
79  * @param string $handle Name of the stylesheet to add the extra styles to. Must be lowercase.
80  * @param string $data   String containing the CSS styles to be added.
81  * @return bool True on success, false on failure.
82  */
83 function wp_add_inline_style( $handle, $data ) {
84         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
85
86         if ( false !== stripos( $data, '</style>' ) ) {
87                 _doing_it_wrong( __FUNCTION__, __( 'Do not pass style tags to wp_add_inline_style().' ), '3.7' );
88                 $data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) );
89         }
90
91         return wp_styles()->add_inline_style( $handle, $data );
92 }
93
94 /**
95  * Register a CSS stylesheet.
96  *
97  * @see WP_Dependencies::add()
98  * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
99  *
100  * @since 2.6.0
101  * @since 4.3.0 A return value was added.
102  *
103  * @param string      $handle Name of the stylesheet.
104  * @param string|bool $src    Path to the stylesheet from the WordPress root directory. Example: '/css/mystyle.css'.
105  * @param array       $deps   An array of registered style handles this stylesheet depends on. Default empty array.
106  * @param string|bool $ver    String specifying the stylesheet version number. Used to ensure that the correct version
107  *                            is sent to the client regardless of caching. Default 'false'. Accepts 'false', 'null', or 'string'.
108  * @param string      $media  Optional. The media for which this stylesheet has been defined.
109  *                            Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print',
110  *                            'screen', 'tty', or 'tv'.
111  * @return bool Whether the style has been registered. True on success, false on failure.
112  */
113 function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
114         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
115
116         return wp_styles()->add( $handle, $src, $deps, $ver, $media );
117 }
118
119 /**
120  * Remove a registered stylesheet.
121  *
122  * @see WP_Dependencies::remove()
123  *
124  * @since 2.1.0
125  *
126  * @param string $handle Name of the stylesheet to be removed.
127  */
128 function wp_deregister_style( $handle ) {
129         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
130
131         wp_styles()->remove( $handle );
132 }
133
134 /**
135  * Enqueue a CSS stylesheet.
136  *
137  * Registers the style if source provided (does NOT overwrite) and enqueues.
138  *
139  * @see WP_Dependencies::add(), WP_Dependencies::enqueue()
140  * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
141  *
142  * @since 2.6.0
143  *
144  * @param string      $handle Name of the stylesheet.
145  * @param string|bool $src    Path to the stylesheet from the root directory of WordPress. Example: '/css/mystyle.css'.
146  * @param array       $deps   An array of registered style handles this stylesheet depends on. Default empty array.
147  * @param string|bool $ver    String specifying the stylesheet version number, if it has one. This parameter is used
148  *                            to ensure that the correct version is sent to the client regardless of caching, and so
149  *                            should be included if a version number is available and makes sense for the stylesheet.
150  * @param string      $media  Optional. The media for which this stylesheet has been defined.
151  *                            Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print',
152  *                            'screen', 'tty', or 'tv'.
153  */
154 function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
155         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
156
157         $wp_styles = wp_styles();
158
159         if ( $src ) {
160                 $_handle = explode('?', $handle);
161                 $wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
162         }
163         $wp_styles->enqueue( $handle );
164 }
165
166 /**
167  * Remove a previously enqueued CSS stylesheet.
168  *
169  * @see WP_Dependencies::dequeue()
170  *
171  * @since 3.1.0
172  *
173  * @param string $handle Name of the stylesheet to be removed.
174  */
175 function wp_dequeue_style( $handle ) {
176         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
177
178         wp_styles()->dequeue( $handle );
179 }
180
181 /**
182  * Check whether a CSS stylesheet has been added to the queue.
183  *
184  * @since 2.8.0
185  *
186  * @param string $handle Name of the stylesheet.
187  * @param string $list   Optional. Status of the stylesheet to check. Default 'enqueued'.
188  *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
189  * @return bool Whether style is queued.
190  */
191 function wp_style_is( $handle, $list = 'enqueued' ) {
192         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
193
194         return (bool) wp_styles()->query( $handle, $list );
195 }
196
197 /**
198  * Add metadata to a CSS stylesheet.
199  *
200  * Works only if the stylesheet has already been added.
201  *
202  * Possible values for $key and $value:
203  * 'conditional' string      Comments for IE 6, lte IE 7 etc.
204  * 'rtl'         bool|string To declare an RTL stylesheet.
205  * 'suffix'      string      Optional suffix, used in combination with RTL.
206  * 'alt'         bool        For rel="alternate stylesheet".
207  * 'title'       string      For preferred/alternate stylesheets.
208  *
209  * @see WP_Dependency::add_data()
210  *
211  * @since 3.6.0
212  *
213  * @param string $handle Name of the stylesheet.
214  * @param string $key    Name of data point for which we're storing a value.
215  *                       Accepts 'conditional', 'rtl' and 'suffix', 'alt' and 'title'.
216  * @param mixed  $value  String containing the CSS data to be added.
217  * @return bool True on success, false on failure.
218  */
219 function wp_style_add_data( $handle, $key, $value ) {
220         return wp_styles()->add_data( $handle, $key, $value );
221 }