Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

408 linhas
9.9 KiB

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