You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

232 lines
7.9 KiB

  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 array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
  39. */
  40. function wp_print_styles( $handles = false ) {
  41. if ( '' === $handles ) { // for wp_head
  42. $handles = false;
  43. }
  44. /**
  45. * Fires before styles in the $handles queue are printed.
  46. *
  47. * @since 2.6.0
  48. */
  49. if ( ! $handles ) {
  50. do_action( 'wp_print_styles' );
  51. }
  52. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  53. global $wp_styles;
  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 in 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__ );
  79. if ( false !== stripos( $data, '</style>' ) ) {
  80. _doing_it_wrong( __FUNCTION__, sprintf(
  81. /* translators: 1: <style>, 2: wp_add_inline_style() */
  82. __( 'Do not pass %1$s tags to %2$s.' ),
  83. '<code>&lt;style&gt;</code>',
  84. '<code>wp_add_inline_style()</code>'
  85. ), '3.7.0' );
  86. $data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) );
  87. }
  88. return wp_styles()->add_inline_style( $handle, $data );
  89. }
  90. /**
  91. * Register a CSS stylesheet.
  92. *
  93. * @see WP_Dependencies::add()
  94. * @link https://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
  95. *
  96. * @since 2.6.0
  97. * @since 4.3.0 A return value was added.
  98. *
  99. * @param string $handle Name of the stylesheet. Should be unique.
  100. * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
  101. * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
  102. * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
  103. * as a query string for cache busting purposes. If version is set to false, a version
  104. * number is automatically added equal to current installed WordPress version.
  105. * If set to null, no version is added.
  106. * @param string $media Optional. The media for which this stylesheet has been defined.
  107. * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
  108. * '(orientation: portrait)' and '(max-width: 640px)'.
  109. * @return bool Whether the style has been registered. True on success, false on failure.
  110. */
  111. function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
  112. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  113. return wp_styles()->add( $handle, $src, $deps, $ver, $media );
  114. }
  115. /**
  116. * Remove a registered stylesheet.
  117. *
  118. * @see WP_Dependencies::remove()
  119. *
  120. * @since 2.1.0
  121. *
  122. * @param string $handle Name of the stylesheet to be removed.
  123. */
  124. function wp_deregister_style( $handle ) {
  125. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  126. wp_styles()->remove( $handle );
  127. }
  128. /**
  129. * Enqueue a CSS stylesheet.
  130. *
  131. * Registers the style if source provided (does NOT overwrite) and enqueues.
  132. *
  133. * @see WP_Dependencies::add()
  134. * @see WP_Dependencies::enqueue()
  135. * @link https://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
  136. *
  137. * @since 2.6.0
  138. *
  139. * @param string $handle Name of the stylesheet. Should be unique.
  140. * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
  141. * Default empty.
  142. * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
  143. * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
  144. * as a query string for cache busting purposes. If version is set to false, a version
  145. * number is automatically added equal to current installed WordPress version.
  146. * If set to null, no version is added.
  147. * @param string $media Optional. The media for which this stylesheet has been defined.
  148. * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
  149. * '(orientation: portrait)' and '(max-width: 640px)'.
  150. */
  151. function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) {
  152. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  153. $wp_styles = wp_styles();
  154. if ( $src ) {
  155. $_handle = explode('?', $handle);
  156. $wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
  157. }
  158. $wp_styles->enqueue( $handle );
  159. }
  160. /**
  161. * Remove a previously enqueued CSS stylesheet.
  162. *
  163. * @see WP_Dependencies::dequeue()
  164. *
  165. * @since 3.1.0
  166. *
  167. * @param string $handle Name of the stylesheet to be removed.
  168. */
  169. function wp_dequeue_style( $handle ) {
  170. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  171. wp_styles()->dequeue( $handle );
  172. }
  173. /**
  174. * Check whether a CSS stylesheet has been added to the queue.
  175. *
  176. * @since 2.8.0
  177. *
  178. * @param string $handle Name of the stylesheet.
  179. * @param string $list Optional. Status of the stylesheet to check. Default 'enqueued'.
  180. * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
  181. * @return bool Whether style is queued.
  182. */
  183. function wp_style_is( $handle, $list = 'enqueued' ) {
  184. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
  185. return (bool) wp_styles()->query( $handle, $list );
  186. }
  187. /**
  188. * Add metadata to a CSS stylesheet.
  189. *
  190. * Works only if the stylesheet has already been added.
  191. *
  192. * Possible values for $key and $value:
  193. * 'conditional' string Comments for IE 6, lte IE 7 etc.
  194. * 'rtl' bool|string To declare an RTL stylesheet.
  195. * 'suffix' string Optional suffix, used in combination with RTL.
  196. * 'alt' bool For rel="alternate stylesheet".
  197. * 'title' string For preferred/alternate stylesheets.
  198. *
  199. * @see WP_Dependency::add_data()
  200. *
  201. * @since 3.6.0
  202. *
  203. * @param string $handle Name of the stylesheet.
  204. * @param string $key Name of data point for which we're storing a value.
  205. * Accepts 'conditional', 'rtl' and 'suffix', 'alt' and 'title'.
  206. * @param mixed $value String containing the CSS data to be added.
  207. * @return bool True on success, false on failure.
  208. */
  209. function wp_style_add_data( $handle, $key, $value ) {
  210. return wp_styles()->add_data( $handle, $key, $value );
  211. }