class-wp-styles.php 11 KB

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