| 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 | * Initializes $wp_styles if it has not been set. |
|---|
| 13 | * |
|---|
| 14 | * @since 4.2.0 |
|---|
| 15 | * |
|---|
| 16 | * @global WP_Styles $wp_styles |
|---|
| 17 | * |
|---|
| 18 | * @return WP_Styles WP_Styles instance. |
|---|
| 19 | */ |
|---|
| 20 | function wp_styles() { |
|---|
| 21 | global $wp_styles; |
|---|
| 22 | |
|---|
| 23 | if ( ! ( $wp_styles instanceof WP_Styles ) ) { |
|---|
| 24 | $wp_styles = new WP_Styles(); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | return $wp_styles; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Displays styles that are in the $handles queue. |
|---|
| 32 | * |
|---|
| 33 | * Passing an empty array to $handles prints the queue, |
|---|
| 34 | * passing an array with one string prints that style, |
|---|
| 35 | * and passing an array of strings prints those styles. |
|---|
| 36 | * |
|---|
| 37 | * @since 2.6.0 |
|---|
| 38 | * |
|---|
| 39 | * @global WP_Styles $wp_styles The WP_Styles object for printing styles. |
|---|
| 40 | * |
|---|
| 41 | * @param string|bool|array $handles Styles to be printed. Default 'false'. |
|---|
| 42 | * @return string[] On success, an array of handles of processed WP_Dependencies items; otherwise, an empty array. |
|---|
| 43 | */ |
|---|
| 44 | function wp_print_styles( $handles = false ) { |
|---|
| 45 | global $wp_styles; |
|---|
| 46 | |
|---|
| 47 | if ( '' === $handles ) { // For 'wp_head'. |
|---|
| 48 | $handles = false; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | if ( ! $handles ) { |
|---|
| 52 | /** |
|---|
| 53 | * Fires before styles in the $handles queue are printed. |
|---|
| 54 | * |
|---|
| 55 | * @since 2.6.0 |
|---|
| 56 | */ |
|---|
| 57 | do_action( 'wp_print_styles' ); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
|---|
| 61 | |
|---|
| 62 | if ( ! ( $wp_styles instanceof WP_Styles ) ) { |
|---|
| 63 | if ( ! $handles ) { |
|---|
| 64 | return array(); // No need to instantiate if nothing is there. |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | return wp_styles()->do_items( $handles ); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * Adds extra CSS styles to a registered stylesheet. |
|---|
| 73 | * |
|---|
| 74 | * Styles will only be added if the stylesheet is already in the queue. |
|---|
| 75 | * Accepts a string $data containing the CSS. If two or more CSS code blocks |
|---|
| 76 | * are added to the same stylesheet $handle, they will be printed in the order |
|---|
| 77 | * they were added, i.e. the latter added styles can redeclare the previous. |
|---|
| 78 | * |
|---|
| 79 | * @see WP_Styles::add_inline_style() |
|---|
| 80 | * |
|---|
| 81 | * @since 3.3.0 |
|---|
| 82 | * |
|---|
| 83 | * @param string $handle Name of the stylesheet to add the extra styles to. |
|---|
| 84 | * @param string $data String containing the CSS styles to be added. |
|---|
| 85 | * @return bool True on success, false on failure. |
|---|
| 86 | */ |
|---|
| 87 | function wp_add_inline_style( $handle, $data ) { |
|---|
| 88 | _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
|---|
| 89 | |
|---|
| 90 | if ( ! is_string( $data ) || empty( trim( $data ) ) ) { |
|---|
| 91 | _doing_it_wrong( __FUNCTION__, __( 'The $data parameter must be a non-empty string.' ), '6.8.3' ); // Adjusted version |
|---|
| 92 | return false; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | if ( false !== stripos( $data, '</style>' ) ) { |
|---|
| 96 | _doing_it_wrong( |
|---|
| 97 | __FUNCTION__, |
|---|
| 98 | sprintf( |
|---|
| 99 | /* translators: 1: <style>, 2: wp_add_inline_style() */ |
|---|
| 100 | __( 'Do not pass %1$s tags to %2$s.' ), |
|---|
| 101 | '<code><style></code>', |
|---|
| 102 | '<code>wp_add_inline_style()</code>' |
|---|
| 103 | ), |
|---|
| 104 | '3.7.0' |
|---|
| 105 | ); |
|---|
| 106 | $data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) ); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | return wp_styles()->add_inline_style( $handle, $data ); |
|---|
| 110 | } |
|---|
| 111 | /** |
|---|
| 112 | * Registers a CSS stylesheet. |
|---|
| 113 | * |
|---|
| 114 | * @see WP_Dependencies::add() |
|---|
| 115 | * @link https://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types. |
|---|
| 116 | * |
|---|
| 117 | * @since 2.6.0 |
|---|
| 118 | * @since 4.3.0 A return value was added. |
|---|
| 119 | * |
|---|
| 120 | * @param string $handle Name of the stylesheet. Should be unique. |
|---|
| 121 | * @param string|false $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
|---|
| 122 | * If source is set to false, stylesheet is an alias of other stylesheets it depends on. |
|---|
| 123 | * @param string[] $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
|---|
| 124 | * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL |
|---|
| 125 | * as a query string for cache busting purposes. If version is set to false, a version |
|---|
| 126 | * number is automatically added equal to current installed WordPress version. |
|---|
| 127 | * If set to null, no version is added. |
|---|
| 128 | * @param string $media Optional. The media for which this stylesheet has been defined. |
|---|
| 129 | * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like |
|---|
| 130 | * '(orientation: portrait)' and '(max-width: 640px)'. |
|---|
| 131 | * @return bool Whether the style has been registered. True on success, false on failure. |
|---|
| 132 | */ |
|---|
| 133 | function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) { |
|---|
| 134 | _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
|---|
| 135 | |
|---|
| 136 | return wp_styles()->add( $handle, $src, $deps, $ver, $media ); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * Removes a registered stylesheet. |
|---|
| 141 | * |
|---|
| 142 | * @see WP_Dependencies::remove() |
|---|
| 143 | * |
|---|
| 144 | * @since 2.1.0 |
|---|
| 145 | * |
|---|
| 146 | * @param string $handle Name of the stylesheet to be removed. |
|---|
| 147 | */ |
|---|
| 148 | function wp_deregister_style( $handle ) { |
|---|
| 149 | _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
|---|
| 150 | |
|---|
| 151 | wp_styles()->remove( $handle ); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * Enqueues a CSS stylesheet. |
|---|
| 156 | * |
|---|
| 157 | * Registers the style if source provided (does NOT overwrite) and enqueues. |
|---|
| 158 | * |
|---|
| 159 | * @see WP_Dependencies::add() |
|---|
| 160 | * @see WP_Dependencies::enqueue() |
|---|
| 161 | * @link https://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types. |
|---|
| 162 | * |
|---|
| 163 | * @since 2.6.0 |
|---|
| 164 | * |
|---|
| 165 | * @param string $handle Name of the stylesheet. Should be unique. |
|---|
| 166 | * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. |
|---|
| 167 | * Default empty. |
|---|
| 168 | * @param string[] $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. |
|---|
| 169 | * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL |
|---|
| 170 | * as a query string for cache busting purposes. If version is set to false, a version |
|---|
| 171 | * number is automatically added equal to current installed WordPress version. |
|---|
| 172 | * If set to null, no version is added. |
|---|
| 173 | * @param string $media Optional. The media for which this stylesheet has been defined. |
|---|
| 174 | * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like |
|---|
| 175 | * '(orientation: portrait)' and '(max-width: 640px)'. |
|---|
| 176 | */ |
|---|
| 177 | function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) { |
|---|
| 178 | _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
|---|
| 179 | |
|---|
| 180 | $wp_styles = wp_styles(); |
|---|
| 181 | |
|---|
| 182 | if ( $src ) { |
|---|
| 183 | $_handle = explode( '?', $handle ); |
|---|
| 184 | $wp_styles->add( $_handle[0], $src, $deps, $ver, $media ); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | $wp_styles->enqueue( $handle ); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | /** |
|---|
| 191 | * Removes a previously enqueued CSS stylesheet. |
|---|
| 192 | * |
|---|
| 193 | * @see WP_Dependencies::dequeue() |
|---|
| 194 | * |
|---|
| 195 | * @since 3.1.0 |
|---|
| 196 | * |
|---|
| 197 | * @param string $handle Name of the stylesheet to be removed. |
|---|
| 198 | */ |
|---|
| 199 | function wp_dequeue_style( $handle ) { |
|---|
| 200 | _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
|---|
| 201 | |
|---|
| 202 | wp_styles()->dequeue( $handle ); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * Checks whether a CSS stylesheet has been added to the queue. |
|---|
| 207 | * |
|---|
| 208 | * @since 2.8.0 |
|---|
| 209 | * |
|---|
| 210 | * @param string $handle Name of the stylesheet. |
|---|
| 211 | * @param string $status Optional. Status of the stylesheet to check. Default 'enqueued'. |
|---|
| 212 | * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'. |
|---|
| 213 | * @return bool Whether style is queued. |
|---|
| 214 | */ |
|---|
| 215 | function wp_style_is( $handle, $status = 'enqueued' ) { |
|---|
| 216 | _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
|---|
| 217 | |
|---|
| 218 | return (bool) wp_styles()->query( $handle, $status ); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /** |
|---|
| 222 | * Adds metadata to a CSS stylesheet. |
|---|
| 223 | * |
|---|
| 224 | * Works only if the stylesheet has already been registered. |
|---|
| 225 | * |
|---|
| 226 | * Possible values for $key and $value: |
|---|
| 227 | * 'conditional' string Comments for IE 6, lte IE 7 etc. |
|---|
| 228 | * 'rtl' bool|string To declare an RTL stylesheet. |
|---|
| 229 | * 'suffix' string Optional suffix, used in combination with RTL. |
|---|
| 230 | * 'alt' bool For rel="alternate stylesheet". |
|---|
| 231 | * 'title' string For preferred/alternate stylesheets. |
|---|
| 232 | * 'path' string The absolute path to a stylesheet. Stylesheet will |
|---|
| 233 | * load inline when 'path' is set. |
|---|
| 234 | * |
|---|
| 235 | * @see WP_Dependencies::add_data() |
|---|
| 236 | * |
|---|
| 237 | * @since 3.6.0 |
|---|
| 238 | * @since 5.8.0 Added 'path' as an official value for $key. |
|---|
| 239 | * See {@see wp_maybe_inline_styles()}. |
|---|
| 240 | * |
|---|
| 241 | * @param string $handle Name of the stylesheet. |
|---|
| 242 | * @param string $key Name of data point for which we're storing a value. |
|---|
| 243 | * Accepts 'conditional', 'rtl' and 'suffix', 'alt', 'title' and 'path'. |
|---|
| 244 | * @param mixed $value String containing the CSS data to be added. |
|---|
| 245 | * @return bool True on success, false on failure. |
|---|
| 246 | */ |
|---|
| 247 | function wp_style_add_data( $handle, $key, $value ) { |
|---|
| 248 | return wp_styles()->add_data( $handle, $key, $value ); |
|---|
| 249 | } |
|---|