functions.wp-styles.php ➔ wp_style_is()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Dependencies API: Styles functions
4
 *
5
 * @since 2.6.0
6
 *
7
 * @package WordPress
8
 * @subpackage Dependencies
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 View Code Duplication
function wp_print_styles( $handles = false ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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.
80
 * @param string $data   String containing the CSS styles to be added.
81
 * @return bool True on success, false on failure.
82
 */
83 View Code Duplication
function wp_add_inline_style( $handle, $data ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
85
86
	if ( false !== stripos( $data, '</style>' ) ) {
87
		_doing_it_wrong( __FUNCTION__, sprintf(
88
			/* translators: 1: <style>, 2: wp_add_inline_style() */
89
			__( 'Do not pass %1$s tags to %2$s.' ),
90
			'<code>&lt;style&gt;</code>',
91
			'<code>wp_add_inline_style()</code>'
92
		), '3.7.0' );
93
		$data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) );
94
	}
95
96
	return wp_styles()->add_inline_style( $handle, $data );
97
}
98
99
/**
100
 * Register a CSS stylesheet.
101
 *
102
 * @see WP_Dependencies::add()
103
 * @link https://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
104
 *
105
 * @since 2.6.0
106
 * @since 4.3.0 A return value was added.
107
 *
108
 * @param string           $handle Name of the stylesheet. Should be unique.
109
 * @param string           $src    Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
110
 * @param array            $deps   Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
111
 * @param string|bool|null $ver    Optional. String specifying stylesheet version number, if it has one, which is added to the URL
112
 *                                 as a query string for cache busting purposes. If version is set to false, a version
113
 *                                 number is automatically added equal to current installed WordPress version.
114
 *                                 If set to null, no version is added.
115
 * @param string           $media  Optional. The media for which this stylesheet has been defined.
116
 *                                 Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
117
 *                                 '(orientation: portrait)' and '(max-width: 640px)'.
118
 * @return bool Whether the style has been registered. True on success, false on failure.
119
 */
120
function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
121
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
122
123
	return wp_styles()->add( $handle, $src, $deps, $ver, $media );
124
}
125
126
/**
127
 * Remove a registered stylesheet.
128
 *
129
 * @see WP_Dependencies::remove()
130
 *
131
 * @since 2.1.0
132
 *
133
 * @param string $handle Name of the stylesheet to be removed.
134
 */
135
function wp_deregister_style( $handle ) {
136
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
137
138
	wp_styles()->remove( $handle );
139
}
140
141
/**
142
 * Enqueue a CSS stylesheet.
143
 *
144
 * Registers the style if source provided (does NOT overwrite) and enqueues.
145
 *
146
 * @see WP_Dependencies::add()
147
 * @see WP_Dependencies::enqueue()
148
 * @link https://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
149
 *
150
 * @since 2.6.0
151
 *
152
 * @param string           $handle Name of the stylesheet. Should be unique.
153
 * @param string           $src    Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
154
 * @param array            $deps   Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
155
 * @param string|bool|null $ver    Optional. String specifying stylesheet version number, if it has one, which is added to the URL
156
 *                                 as a query string for cache busting purposes. If version is set to false, a version
157
 *                                 number is automatically added equal to current installed WordPress version.
158
 *                                 If set to null, no version is added.
159
 * @param string           $media  Optional. The media for which this stylesheet has been defined.
160
 *                                 Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
161
 *                                 '(orientation: portrait)' and '(max-width: 640px)'.
162
 */
163
function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
164
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
165
166
	$wp_styles = wp_styles();
167
168
	if ( $src ) {
169
		$_handle = explode('?', $handle);
170
		$wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
171
	}
172
	$wp_styles->enqueue( $handle );
173
}
174
175
/**
176
 * Remove a previously enqueued CSS stylesheet.
177
 *
178
 * @see WP_Dependencies::dequeue()
179
 *
180
 * @since 3.1.0
181
 *
182
 * @param string $handle Name of the stylesheet to be removed.
183
 */
184
function wp_dequeue_style( $handle ) {
185
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
186
187
	wp_styles()->dequeue( $handle );
188
}
189
190
/**
191
 * Check whether a CSS stylesheet has been added to the queue.
192
 *
193
 * @since 2.8.0
194
 *
195
 * @param string $handle Name of the stylesheet.
196
 * @param string $list   Optional. Status of the stylesheet to check. Default 'enqueued'.
197
 *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
198
 * @return bool Whether style is queued.
199
 */
200
function wp_style_is( $handle, $list = 'enqueued' ) {
201
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
202
203
	return (bool) wp_styles()->query( $handle, $list );
204
}
205
206
/**
207
 * Add metadata to a CSS stylesheet.
208
 *
209
 * Works only if the stylesheet has already been added.
210
 *
211
 * Possible values for $key and $value:
212
 * 'conditional' string      Comments for IE 6, lte IE 7 etc.
213
 * 'rtl'         bool|string To declare an RTL stylesheet.
214
 * 'suffix'      string      Optional suffix, used in combination with RTL.
215
 * 'alt'         bool        For rel="alternate stylesheet".
216
 * 'title'       string      For preferred/alternate stylesheets.
217
 *
218
 * @see WP_Dependency::add_data()
219
 *
220
 * @since 3.6.0
221
 *
222
 * @param string $handle Name of the stylesheet.
223
 * @param string $key    Name of data point for which we're storing a value.
224
 *                       Accepts 'conditional', 'rtl' and 'suffix', 'alt' and 'title'.
225
 * @param mixed  $value  String containing the CSS data to be added.
226
 * @return bool True on success, false on failure.
227
 */
228
function wp_style_add_data( $handle, $key, $value ) {
229
	return wp_styles()->add_data( $handle, $key, $value );
230
}
231