functions.wp-styles.php 8.4 KB

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