PageRenderTime 22ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-includes/class.wp-styles.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 461 lines | 199 code | 55 blank | 207 comment | 45 complexity | 93d18339ddb3ebf040e9bb3a3b69870b MD5 | raw file
  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 current 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 $this 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. *
  129. * @see WP_Dependencies::do_item()
  130. *
  131. * @param string $handle The style's registered handle.
  132. * @return bool True on success, false on failure.
  133. */
  134. public function do_item( $handle ) {
  135. if ( ! parent::do_item( $handle ) ) {
  136. return false;
  137. }
  138. $obj = $this->registered[ $handle ];
  139. if ( null === $obj->ver ) {
  140. $ver = '';
  141. } else {
  142. $ver = $obj->ver ? $obj->ver : $this->default_version;
  143. }
  144. if ( isset( $this->args[ $handle ] ) ) {
  145. $ver = $ver ? $ver . '&amp;' . $this->args[ $handle ] : $this->args[ $handle ];
  146. }
  147. $src = $obj->src;
  148. $cond_before = '';
  149. $cond_after = '';
  150. $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
  151. if ( $conditional ) {
  152. $cond_before = "<!--[if {$conditional}]>\n";
  153. $cond_after = "<![endif]-->\n";
  154. }
  155. $inline_style = $this->print_inline_style( $handle, false );
  156. if ( $inline_style ) {
  157. $inline_style_tag = sprintf(
  158. "<style id='%s-inline-css'%s>\n%s\n</style>\n",
  159. esc_attr( $handle ),
  160. $this->type_attr,
  161. $inline_style
  162. );
  163. } else {
  164. $inline_style_tag = '';
  165. }
  166. if ( $this->do_concat ) {
  167. if ( $this->in_default_dir( $src ) && ! $conditional && ! isset( $obj->extra['alt'] ) ) {
  168. $this->concat .= "$handle,";
  169. $this->concat_version .= "$handle$ver";
  170. $this->print_code .= $inline_style;
  171. return true;
  172. }
  173. }
  174. if ( isset( $obj->args ) ) {
  175. $media = esc_attr( $obj->args );
  176. } else {
  177. $media = 'all';
  178. }
  179. // A single item may alias a set of items, by having dependencies, but no source.
  180. if ( ! $src ) {
  181. if ( $inline_style_tag ) {
  182. if ( $this->do_concat ) {
  183. $this->print_html .= $inline_style_tag;
  184. } else {
  185. echo $inline_style_tag;
  186. }
  187. }
  188. return true;
  189. }
  190. $href = $this->_css_href( $src, $ver, $handle );
  191. if ( ! $href ) {
  192. return true;
  193. }
  194. $rel = isset( $obj->extra['alt'] ) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
  195. $title = isset( $obj->extra['title'] ) ? sprintf( "title='%s'", esc_attr( $obj->extra['title'] ) ) : '';
  196. $tag = sprintf(
  197. "<link rel='%s' id='%s-css' %s href='%s'%s media='%s' />\n",
  198. $rel,
  199. $handle,
  200. $title,
  201. $href,
  202. $this->type_attr,
  203. $media
  204. );
  205. /**
  206. * Filters the HTML link tag of an enqueued style.
  207. *
  208. * @since 2.6.0
  209. * @since 4.3.0 Introduced the `$href` parameter.
  210. * @since 4.5.0 Introduced the `$media` parameter.
  211. *
  212. * @param string $html The link tag for the enqueued style.
  213. * @param string $handle The style's registered handle.
  214. * @param string $href The stylesheet's source URL.
  215. * @param string $media The stylesheet's media attribute.
  216. */
  217. $tag = apply_filters( 'style_loader_tag', $tag, $handle, $href, $media );
  218. if ( 'rtl' === $this->text_direction && isset( $obj->extra['rtl'] ) && $obj->extra['rtl'] ) {
  219. if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
  220. $suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
  221. $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $src, $ver, "$handle-rtl" ) );
  222. } else {
  223. $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
  224. }
  225. $rtl_tag = sprintf(
  226. "<link rel='%s' id='%s-rtl-css' %s href='%s'%s media='%s' />\n",
  227. $rel,
  228. $handle,
  229. $title,
  230. $rtl_href,
  231. $this->type_attr,
  232. $media
  233. );
  234. /** This filter is documented in wp-includes/class.wp-styles.php */
  235. $rtl_tag = apply_filters( 'style_loader_tag', $rtl_tag, $handle, $rtl_href, $media );
  236. if ( 'replace' === $obj->extra['rtl'] ) {
  237. $tag = $rtl_tag;
  238. } else {
  239. $tag .= $rtl_tag;
  240. }
  241. }
  242. if ( $this->do_concat ) {
  243. $this->print_html .= $cond_before;
  244. $this->print_html .= $tag;
  245. if ( $inline_style_tag ) {
  246. $this->print_html .= $inline_style_tag;
  247. }
  248. $this->print_html .= $cond_after;
  249. } else {
  250. echo $cond_before;
  251. echo $tag;
  252. $this->print_inline_style( $handle );
  253. echo $cond_after;
  254. }
  255. return true;
  256. }
  257. /**
  258. * Adds extra CSS styles to a registered stylesheet.
  259. *
  260. * @since 3.3.0
  261. *
  262. * @param string $handle The style's registered handle.
  263. * @param string $code String containing the CSS styles to be added.
  264. * @return bool True on success, false on failure.
  265. */
  266. public function add_inline_style( $handle, $code ) {
  267. if ( ! $code ) {
  268. return false;
  269. }
  270. $after = $this->get_data( $handle, 'after' );
  271. if ( ! $after ) {
  272. $after = array();
  273. }
  274. $after[] = $code;
  275. return $this->add_data( $handle, 'after', $after );
  276. }
  277. /**
  278. * Prints extra CSS styles of a registered stylesheet.
  279. *
  280. * @since 3.3.0
  281. *
  282. * @param string $handle The style's registered handle.
  283. * @param bool $echo Optional. Whether to echo the inline style
  284. * instead of just returning it. Default true.
  285. * @return string|bool False if no data exists, inline styles if `$echo` is true,
  286. * true otherwise.
  287. */
  288. public function print_inline_style( $handle, $echo = true ) {
  289. $output = $this->get_data( $handle, 'after' );
  290. if ( empty( $output ) ) {
  291. return false;
  292. }
  293. $output = implode( "\n", $output );
  294. if ( ! $echo ) {
  295. return $output;
  296. }
  297. printf(
  298. "<style id='%s-inline-css'%s>\n%s\n</style>\n",
  299. esc_attr( $handle ),
  300. $this->type_attr,
  301. $output
  302. );
  303. return true;
  304. }
  305. /**
  306. * Determines style dependencies.
  307. *
  308. * @since 2.6.0
  309. *
  310. * @see WP_Dependencies::all_deps()
  311. *
  312. * @param string|string[] $handles Item handle (string) or item handles (array of strings).
  313. * @param bool $recursion Optional. Internal flag that function is calling itself.
  314. * Default false.
  315. * @param int|false $group Optional. Group level: level (int), no groups (false).
  316. * Default false.
  317. * @return bool True on success, false on failure.
  318. */
  319. public function all_deps( $handles, $recursion = false, $group = false ) {
  320. $r = parent::all_deps( $handles, $recursion, $group );
  321. if ( ! $recursion ) {
  322. /**
  323. * Filters the array of enqueued styles before processing for output.
  324. *
  325. * @since 2.6.0
  326. *
  327. * @param string[] $to_do The list of enqueued style handles about to be processed.
  328. */
  329. $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
  330. }
  331. return $r;
  332. }
  333. /**
  334. * Generates an enqueued style's fully-qualified URL.
  335. *
  336. * @since 2.6.0
  337. *
  338. * @param string $src The source of the enqueued style.
  339. * @param string $ver The version of the enqueued style.
  340. * @param string $handle The style's registered handle.
  341. * @return string Style's fully-qualified URL.
  342. */
  343. public function _css_href( $src, $ver, $handle ) {
  344. if ( ! is_bool( $src ) && ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
  345. $src = $this->base_url . $src;
  346. }
  347. if ( ! empty( $ver ) ) {
  348. $src = add_query_arg( 'ver', $ver, $src );
  349. }
  350. /**
  351. * Filters an enqueued style's fully-qualified URL.
  352. *
  353. * @since 2.6.0
  354. *
  355. * @param string $src The source URL of the enqueued style.
  356. * @param string $handle The style's registered handle.
  357. */
  358. $src = apply_filters( 'style_loader_src', $src, $handle );
  359. return esc_url( $src );
  360. }
  361. /**
  362. * Whether a handle's source is in a default directory.
  363. *
  364. * @since 2.8.0
  365. *
  366. * @param string $src The source of the enqueued style.
  367. * @return bool True if found, false if not.
  368. */
  369. public function in_default_dir( $src ) {
  370. if ( ! $this->default_dirs ) {
  371. return true;
  372. }
  373. foreach ( (array) $this->default_dirs as $test ) {
  374. if ( 0 === strpos( $src, $test ) ) {
  375. return true;
  376. }
  377. }
  378. return false;
  379. }
  380. /**
  381. * Processes items and dependencies for the footer group.
  382. *
  383. * HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
  384. *
  385. * @since 3.3.0
  386. *
  387. * @see WP_Dependencies::do_items()
  388. *
  389. * @return string[] Handles of items that have been processed.
  390. */
  391. public function do_footer_items() {
  392. $this->do_items( false, 1 );
  393. return $this->done;
  394. }
  395. /**
  396. * Resets class properties.
  397. *
  398. * @since 3.3.0
  399. */
  400. public function reset() {
  401. $this->do_concat = false;
  402. $this->concat = '';
  403. $this->concat_version = '';
  404. $this->print_html = '';
  405. }
  406. }