author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Dependencies API: Styles functions |
0 | 4 |
* |
5 |
* @since 2.6.0 |
|
6 |
* |
|
7 |
* @package WordPress |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
8 |
* @subpackage Dependencies |
0 | 9 |
*/ |
10 |
||
11 |
/** |
|
5 | 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 |
/** |
|
0 | 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 |
* |
|
5 | 39 |
* @param string|bool|array $handles Styles to be printed. Default 'false'. |
0 | 40 |
* @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array. |
41 |
*/ |
|
42 |
function wp_print_styles( $handles = false ) { |
|
5 | 43 |
if ( '' === $handles ) { // for wp_head |
0 | 44 |
$handles = false; |
5 | 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 |
} |
|
0 | 54 |
|
5 | 55 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
0 | 56 |
|
57 |
global $wp_styles; |
|
5 | 58 |
if ( ! ( $wp_styles instanceof WP_Styles ) ) { |
59 |
if ( ! $handles ) { |
|
0 | 60 |
return array(); // No need to instantiate if nothing is there. |
5 | 61 |
} |
0 | 62 |
} |
63 |
||
5 | 64 |
return wp_styles()->do_items( $handles ); |
0 | 65 |
} |
66 |
||
67 |
/** |
|
68 |
* Add extra CSS styles to a registered stylesheet. |
|
69 |
* |
|
9 | 70 |
* Styles will only be added if the stylesheet is already in the queue. |
0 | 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 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* @param string $handle Name of the stylesheet to add the extra styles to. |
0 | 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 ) { |
|
5 | 84 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
0 | 85 |
|
86 |
if ( false !== stripos( $data, '</style>' ) ) { |
|
9 | 87 |
_doing_it_wrong( |
88 |
__FUNCTION__, |
|
89 |
sprintf( |
|
90 |
/* translators: 1: <style>, 2: wp_add_inline_style() */ |
|
91 |
__( 'Do not pass %1$s tags to %2$s.' ), |
|
92 |
'<code><style></code>', |
|
93 |
'<code>wp_add_inline_style()</code>' |
|
94 |
), |
|
95 |
'3.7.0' |
|
96 |
); |
|
0 | 97 |
$data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) ); |
98 |
} |
|
99 |
||
5 | 100 |
return wp_styles()->add_inline_style( $handle, $data ); |
0 | 101 |
} |
102 |
||
103 |
/** |
|
104 |
* Register a CSS stylesheet. |
|
105 |
* |
|
106 |
* @see WP_Dependencies::add() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
* @link https://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types. |
0 | 108 |
* |
109 |
* @since 2.6.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
* @since 4.3.0 A return value was added. |
0 | 111 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
* @param string $handle Name of the stylesheet. Should be unique. |
9 | 113 |
* @param string|bool $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
114 |
* If source is set to false, stylesheet is an alias of other stylesheets it depends on. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
* @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
* as a query string for cache busting purposes. If version is set to false, a version |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
* number is automatically added equal to current installed WordPress version. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
* If set to null, no version is added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
* @param string $media Optional. The media for which this stylesheet has been defined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
* Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
* '(orientation: portrait)' and '(max-width: 640px)'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
* @return bool Whether the style has been registered. True on success, false on failure. |
0 | 124 |
*/ |
125 |
function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) { |
|
5 | 126 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
0 | 127 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
return wp_styles()->add( $handle, $src, $deps, $ver, $media ); |
0 | 129 |
} |
130 |
||
131 |
/** |
|
132 |
* Remove a registered stylesheet. |
|
133 |
* |
|
134 |
* @see WP_Dependencies::remove() |
|
135 |
* |
|
136 |
* @since 2.1.0 |
|
137 |
* |
|
138 |
* @param string $handle Name of the stylesheet to be removed. |
|
139 |
*/ |
|
140 |
function wp_deregister_style( $handle ) { |
|
5 | 141 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
0 | 142 |
|
5 | 143 |
wp_styles()->remove( $handle ); |
0 | 144 |
} |
145 |
||
146 |
/** |
|
147 |
* Enqueue a CSS stylesheet. |
|
148 |
* |
|
149 |
* Registers the style if source provided (does NOT overwrite) and enqueues. |
|
150 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
* @see WP_Dependencies::add() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* @see WP_Dependencies::enqueue() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* @link https://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types. |
0 | 154 |
* |
155 |
* @since 2.6.0 |
|
156 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
* @param string $handle Name of the stylesheet. Should be unique. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
* @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
* Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
* @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
* as a query string for cache busting purposes. If version is set to false, a version |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
* number is automatically added equal to current installed WordPress version. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
* If set to null, no version is added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
* @param string $media Optional. The media for which this stylesheet has been defined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
* Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
* '(orientation: portrait)' and '(max-width: 640px)'. |
0 | 168 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) { |
5 | 170 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
171 |
||
172 |
$wp_styles = wp_styles(); |
|
0 | 173 |
|
174 |
if ( $src ) { |
|
9 | 175 |
$_handle = explode( '?', $handle ); |
0 | 176 |
$wp_styles->add( $_handle[0], $src, $deps, $ver, $media ); |
177 |
} |
|
178 |
$wp_styles->enqueue( $handle ); |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* Remove a previously enqueued CSS stylesheet. |
|
183 |
* |
|
184 |
* @see WP_Dependencies::dequeue() |
|
185 |
* |
|
186 |
* @since 3.1.0 |
|
187 |
* |
|
188 |
* @param string $handle Name of the stylesheet to be removed. |
|
189 |
*/ |
|
190 |
function wp_dequeue_style( $handle ) { |
|
5 | 191 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
0 | 192 |
|
5 | 193 |
wp_styles()->dequeue( $handle ); |
0 | 194 |
} |
195 |
||
196 |
/** |
|
197 |
* Check whether a CSS stylesheet has been added to the queue. |
|
198 |
* |
|
199 |
* @since 2.8.0 |
|
200 |
* |
|
201 |
* @param string $handle Name of the stylesheet. |
|
202 |
* @param string $list Optional. Status of the stylesheet to check. Default 'enqueued'. |
|
203 |
* Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'. |
|
204 |
* @return bool Whether style is queued. |
|
205 |
*/ |
|
206 |
function wp_style_is( $handle, $list = 'enqueued' ) { |
|
5 | 207 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
0 | 208 |
|
5 | 209 |
return (bool) wp_styles()->query( $handle, $list ); |
0 | 210 |
} |
211 |
||
212 |
/** |
|
213 |
* Add metadata to a CSS stylesheet. |
|
214 |
* |
|
215 |
* Works only if the stylesheet has already been added. |
|
216 |
* |
|
217 |
* Possible values for $key and $value: |
|
218 |
* 'conditional' string Comments for IE 6, lte IE 7 etc. |
|
219 |
* 'rtl' bool|string To declare an RTL stylesheet. |
|
220 |
* 'suffix' string Optional suffix, used in combination with RTL. |
|
221 |
* 'alt' bool For rel="alternate stylesheet". |
|
222 |
* 'title' string For preferred/alternate stylesheets. |
|
223 |
* |
|
224 |
* @see WP_Dependency::add_data() |
|
225 |
* |
|
226 |
* @since 3.6.0 |
|
227 |
* |
|
228 |
* @param string $handle Name of the stylesheet. |
|
229 |
* @param string $key Name of data point for which we're storing a value. |
|
230 |
* Accepts 'conditional', 'rtl' and 'suffix', 'alt' and 'title'. |
|
5 | 231 |
* @param mixed $value String containing the CSS data to be added. |
0 | 232 |
* @return bool True on success, false on failure. |
233 |
*/ |
|
234 |
function wp_style_add_data( $handle, $key, $value ) { |
|
5 | 235 |
return wp_styles()->add_data( $handle, $key, $value ); |
0 | 236 |
} |