| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Dependencies API: WP_Styles class |
|---|
| 4 | * |
|---|
| 5 | * @since 2.6.0 |
|---|
| 6 | * |
|---|
| 7 | * @package WordPress |
|---|
| 8 | * @subpackage Dependencies |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * Core class used to register styles. |
|---|
| 13 | * |
|---|
| 14 | * @since 2.6.0 |
|---|
| 15 | * |
|---|
| 16 | * @see WP_Dependencies |
|---|
| 17 | */ |
|---|
| 18 | class WP_Styles extends WP_Dependencies { |
|---|
| 19 | /** |
|---|
| 20 | * Base URL for styles. |
|---|
| 21 | * |
|---|
| 22 | * Full URL with trailing slash. |
|---|
| 23 | * |
|---|
| 24 | * @since 2.6.0 |
|---|
| 25 | * @var string |
|---|
| 26 | */ |
|---|
| 27 | public $base_url; |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * URL of the content directory. |
|---|
| 31 | * |
|---|
| 32 | * @since 2.8.0 |
|---|
| 33 | * @var string |
|---|
| 34 | */ |
|---|
| 35 | public $content_url; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Default version string for stylesheets. |
|---|
| 39 | * |
|---|
| 40 | * @since 2.6.0 |
|---|
| 41 | * @var string |
|---|
| 42 | */ |
|---|
| 43 | public $default_version; |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * The current text direction. |
|---|
| 47 | * |
|---|
| 48 | * @since 2.6.0 |
|---|
| 49 | * @var string |
|---|
| 50 | */ |
|---|
| 51 | public $text_direction = 'ltr'; |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Holds a list of style handles which will be concatenated. |
|---|
| 55 | * |
|---|
| 56 | * @since 2.8.0 |
|---|
| 57 | * @var string |
|---|
| 58 | */ |
|---|
| 59 | public $concat = ''; |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Holds a string which contains style handles and their version. |
|---|
| 63 | * |
|---|
| 64 | * @since 2.8.0 |
|---|
| 65 | * @deprecated 3.4.0 |
|---|
| 66 | * @var string |
|---|
| 67 | */ |
|---|
| 68 | public $concat_version = ''; |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * Whether to perform concatenation. |
|---|
| 72 | * |
|---|
| 73 | * @since 2.8.0 |
|---|
| 74 | * @var bool |
|---|
| 75 | */ |
|---|
| 76 | public $do_concat = false; |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Holds HTML markup of styles and additional data if concatenation |
|---|
| 80 | * is enabled. |
|---|
| 81 | * |
|---|
| 82 | * @since 2.8.0 |
|---|
| 83 | * @var string |
|---|
| 84 | */ |
|---|
| 85 | public $print_html = ''; |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * Holds inline styles if concatenation is enabled. |
|---|
| 89 | * |
|---|
| 90 | * @since 3.3.0 |
|---|
| 91 | * @var string |
|---|
| 92 | */ |
|---|
| 93 | public $print_code = ''; |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * List of default directories. |
|---|
| 97 | * |
|---|
| 98 | * @since 2.8.0 |
|---|
| 99 | * @var array |
|---|
| 100 | */ |
|---|
| 101 | public $default_dirs; |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Constructor. |
|---|
| 105 | * |
|---|
| 106 | * @since 2.6.0 |
|---|
| 107 | */ |
|---|
| 108 | public function __construct() { |
|---|
| 109 | /** |
|---|
| 110 | * Fires when the WP_Styles instance is initialized. |
|---|
| 111 | * |
|---|
| 112 | * @since 2.6.0 |
|---|
| 113 | * |
|---|
| 114 | * @param WP_Styles $this WP_Styles instance (passed by reference). |
|---|
| 115 | */ |
|---|
| 116 | do_action_ref_array( 'wp_default_styles', array(&$this) ); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Processes a style dependency. |
|---|
| 121 | * |
|---|
| 122 | * @since 2.6.0 |
|---|
| 123 | * |
|---|
| 124 | * @see WP_Dependencies::do_item() |
|---|
| 125 | * |
|---|
| 126 | * @param string $handle The style's registered handle. |
|---|
| 127 | * @return bool True on success, false on failure. |
|---|
| 128 | */ |
|---|
| 129 | public function do_item( $handle ) { |
|---|
| 130 | if ( !parent::do_item($handle) ) |
|---|
| 131 | return false; |
|---|
| 132 | |
|---|
| 133 | $obj = $this->registered[$handle]; |
|---|
| 134 | if ( null === $obj->ver ) |
|---|
| 135 | $ver = ''; |
|---|
| 136 | else |
|---|
| 137 | $ver = $obj->ver ? $obj->ver : $this->default_version; |
|---|
| 138 | |
|---|
| 139 | if ( isset($this->args[$handle]) ) |
|---|
| 140 | $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; |
|---|
| 141 | |
|---|
| 142 | if ( $this->do_concat ) { |
|---|
| 143 | if ( $this->in_default_dir($obj->src) && !isset($obj->extra['conditional']) && !isset($obj->extra['alt']) ) { |
|---|
| 144 | $this->concat .= "$handle,"; |
|---|
| 145 | $this->concat_version .= "$handle$ver"; |
|---|
| 146 | |
|---|
| 147 | $this->print_code .= $this->print_inline_style( $handle, false ); |
|---|
| 148 | |
|---|
| 149 | return true; |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | if ( isset($obj->args) ) |
|---|
| 154 | $media = esc_attr( $obj->args ); |
|---|
| 155 | else |
|---|
| 156 | $media = 'all'; |
|---|
| 157 | |
|---|
| 158 | // A single item may alias a set of items, by having dependencies, but no source. |
|---|
| 159 | if ( ! $obj->src ) { |
|---|
| 160 | if ( $inline_style = $this->print_inline_style( $handle, false ) ) { |
|---|
| 161 | $inline_style = sprintf( "<style id='%s-inline-css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style ); |
|---|
| 162 | if ( $this->do_concat ) { |
|---|
| 163 | $this->print_html .= $inline_style; |
|---|
| 164 | } else { |
|---|
| 165 | echo $inline_style; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | return true; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | $href = $this->_css_href( $obj->src, $ver, $handle ); |
|---|
| 172 | if ( ! $href ) { |
|---|
| 173 | return true; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | $rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet'; |
|---|
| 177 | $title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : ''; |
|---|
| 178 | |
|---|
| 179 | /** |
|---|
| 180 | * Filters the HTML link tag of an enqueued style. |
|---|
| 181 | * |
|---|
| 182 | * @since 2.6.0 |
|---|
| 183 | * @since 4.3.0 Introduced the `$href` parameter. |
|---|
| 184 | * @since 4.5.0 Introduced the `$media` parameter. |
|---|
| 185 | * |
|---|
| 186 | * @param string $html The link tag for the enqueued style. |
|---|
| 187 | * @param string $handle The style's registered handle. |
|---|
| 188 | * @param string $href The stylesheet's source URL. |
|---|
| 189 | * @param string $media The stylesheet's media attribute. |
|---|
| 190 | */ |
|---|
| 191 | $tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' media='$media' />\n", $handle, $href, $media); |
|---|
| 192 | if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) { |
|---|
| 193 | if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) { |
|---|
| 194 | $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : ''; |
|---|
| 195 | $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" )); |
|---|
| 196 | } else { |
|---|
| 197 | $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" ); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /** This filter is documented in wp-includes/class.wp-styles.php */ |
|---|
| 201 | $rtl_tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-rtl-css' $title href='$rtl_href' media='$media' />\n", $handle, $rtl_href, $media ); |
|---|
| 202 | |
|---|
| 203 | if ( $obj->extra['rtl'] === 'replace' ) { |
|---|
| 204 | $tag = $rtl_tag; |
|---|
| 205 | } else { |
|---|
| 206 | $tag .= $rtl_tag; |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | $conditional_pre = $conditional_post = ''; |
|---|
| 211 | if ( isset( $obj->extra['conditional'] ) && $obj->extra['conditional'] ) { |
|---|
| 212 | $conditional_pre = "<!--[if {$obj->extra['conditional']}]>\n"; |
|---|
| 213 | $conditional_post = "<![endif]-->\n"; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | if ( $this->do_concat ) { |
|---|
| 217 | $this->print_html .= $conditional_pre; |
|---|
| 218 | $this->print_html .= $tag; |
|---|
| 219 | if ( $inline_style = $this->print_inline_style( $handle, false ) ) { |
|---|
| 220 | $this->print_html .= sprintf( "<style id='%s-inline-css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style ); |
|---|
| 221 | } |
|---|
| 222 | $this->print_html .= $conditional_post; |
|---|
| 223 | } else { |
|---|
| 224 | echo $conditional_pre; |
|---|
| 225 | echo $tag; |
|---|
| 226 | $this->print_inline_style( $handle ); |
|---|
| 227 | echo $conditional_post; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | return true; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 234 | * Adds extra CSS styles to a registered stylesheet. |
|---|
| 235 | * |
|---|
| 236 | * @since 3.3.0 |
|---|
| 237 | * |
|---|
| 238 | * @param string $handle The style's registered handle. |
|---|
| 239 | * @param string $code String containing the CSS styles to be added. |
|---|
| 240 | * @return bool True on success, false on failure. |
|---|
| 241 | */ |
|---|
| 242 | public function add_inline_style( $handle, $code ) { |
|---|
| 243 | if ( ! $code ) { |
|---|
| 244 | return false; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | $after = $this->get_data( $handle, 'after' ); |
|---|
| 248 | if ( ! $after ) { |
|---|
| 249 | $after = array(); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | $after[] = $code; |
|---|
| 253 | |
|---|
| 254 | return $this->add_data( $handle, 'after', $after ); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | /** |
|---|
| 258 | * Prints extra CSS styles of a registered stylesheet. |
|---|
| 259 | * |
|---|
| 260 | * @since 3.3.0 |
|---|
| 261 | * |
|---|
| 262 | * @param string $handle The style's registered handle. |
|---|
| 263 | * @param bool $echo Optional. Whether to echo the inline style instead of just returning it. |
|---|
| 264 | * Default true. |
|---|
| 265 | * @return string|bool False if no data exists, inline styles if `$echo` is true, true otherwise. |
|---|
| 266 | */ |
|---|
| 267 | public function print_inline_style( $handle, $echo = true ) { |
|---|
| 268 | $output = $this->get_data( $handle, 'after' ); |
|---|
| 269 | |
|---|
| 270 | if ( empty( $output ) ) { |
|---|
| 271 | return false; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | $output = implode( "\n", $output ); |
|---|
| 275 | |
|---|
| 276 | if ( ! $echo ) { |
|---|
| 277 | return $output; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | printf( "<style id='%s-inline-css'>\n%s\n</style>\n", esc_attr( $handle ), $output ); |
|---|
| 281 | |
|---|
| 282 | return true; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | /** |
|---|
| 286 | * Determines style dependencies. |
|---|
| 287 | * |
|---|
| 288 | * @since 2.6.0 |
|---|
| 289 | * |
|---|
| 290 | * @see WP_Dependencies::all_deps() |
|---|
| 291 | * |
|---|
| 292 | * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
|---|
| 293 | * @param bool $recursion Internal flag that function is calling itself. |
|---|
| 294 | * @param int|false $group Group level: (int) level, (false) no groups. |
|---|
| 295 | * @return bool True on success, false on failure. |
|---|
| 296 | */ |
|---|
| 297 | public function all_deps( $handles, $recursion = false, $group = false ) { |
|---|
| 298 | $r = parent::all_deps( $handles, $recursion, $group ); |
|---|
| 299 | if ( ! $recursion ) { |
|---|
| 300 | /** |
|---|
| 301 | * Filters the array of enqueued styles before processing for output. |
|---|
| 302 | * |
|---|
| 303 | * @since 2.6.0 |
|---|
| 304 | * |
|---|
| 305 | * @param array $to_do The list of enqueued styles about to be processed. |
|---|
| 306 | */ |
|---|
| 307 | $this->to_do = apply_filters( 'print_styles_array', $this->to_do ); |
|---|
| 308 | } |
|---|
| 309 | return $r; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | /** |
|---|
| 313 | * Generates an enqueued style's fully-qualified URL. |
|---|
| 314 | * |
|---|
| 315 | * @since 2.6.0 |
|---|
| 316 | * |
|---|
| 317 | * @param string $src The source of the enqueued style. |
|---|
| 318 | * @param string $ver The version of the enqueued style. |
|---|
| 319 | * @param string $handle The style's registered handle. |
|---|
| 320 | * @return string Style's fully-qualified URL. |
|---|
| 321 | */ |
|---|
| 322 | public function _css_href( $src, $ver, $handle ) { |
|---|
| 323 | if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) { |
|---|
| 324 | $src = $this->base_url . $src; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | if ( !empty($ver) ) |
|---|
| 328 | $src = add_query_arg('ver', $ver, $src); |
|---|
| 329 | |
|---|
| 330 | /** |
|---|
| 331 | * Filters an enqueued style's fully-qualified URL. |
|---|
| 332 | * |
|---|
| 333 | * @since 2.6.0 |
|---|
| 334 | * |
|---|
| 335 | * @param string $src The source URL of the enqueued style. |
|---|
| 336 | * @param string $handle The style's registered handle. |
|---|
| 337 | */ |
|---|
| 338 | $src = apply_filters( 'style_loader_src', $src, $handle ); |
|---|
| 339 | return esc_url( $src ); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | /** |
|---|
| 343 | * Whether a handle's source is in a default directory. |
|---|
| 344 | * |
|---|
| 345 | * @since 2.8.0 |
|---|
| 346 | * |
|---|
| 347 | * @param string $src The source of the enqueued style. |
|---|
| 348 | * @return bool True if found, false if not. |
|---|
| 349 | */ |
|---|
| 350 | public function in_default_dir( $src ) { |
|---|
| 351 | if ( ! $this->default_dirs ) |
|---|
| 352 | return true; |
|---|
| 353 | |
|---|
| 354 | foreach ( (array) $this->default_dirs as $test ) { |
|---|
| 355 | if ( 0 === strpos($src, $test) ) |
|---|
| 356 | return true; |
|---|
| 357 | } |
|---|
| 358 | return false; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | /** |
|---|
| 362 | * Processes items and dependencies for the footer group. |
|---|
| 363 | * |
|---|
| 364 | * HTML 5 allows styles in the body, grab late enqueued items and output them in the footer. |
|---|
| 365 | * |
|---|
| 366 | * @since 3.3.0 |
|---|
| 367 | * |
|---|
| 368 | * @see WP_Dependencies::do_items() |
|---|
| 369 | * |
|---|
| 370 | * @return array Handles of items that have been processed. |
|---|
| 371 | */ |
|---|
| 372 | public function do_footer_items() { |
|---|
| 373 | $this->do_items(false, 1); |
|---|
| 374 | return $this->done; |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | /** |
|---|
| 378 | * Resets class properties. |
|---|
| 379 | * |
|---|
| 380 | * @since 3.3.0 |
|---|
| 381 | */ |
|---|
| 382 | public function reset() { |
|---|
| 383 | $this->do_concat = false; |
|---|
| 384 | $this->concat = ''; |
|---|
| 385 | $this->concat_version = ''; |
|---|
| 386 | $this->print_html = ''; |
|---|
| 387 | } |
|---|
| 388 | } |
|---|