/wp-includes/functions.wp-styles.php

  1. <?php 
  2. /** 
  3. * Dependencies API: Styles functions 
  4. * 
  5. * @since 2.6.0 
  6. * 
  7. * @package WordPress 
  8. * @subpackage Dependencies 
  9. */ 
  10.  
  11. /** 
  12. * Initialize $wp_styles if it has not been set. 
  13. * 
  14. * @global WP_Styles $wp_styles 
  15. * 
  16. * @since 4.2.0 
  17. * 
  18. * @return WP_Styles WP_Styles instance. 
  19. */ 
  20. function wp_styles() { 
  21. global $wp_styles; 
  22. if ( ! ( $wp_styles instanceof WP_Styles ) ) { 
  23. $wp_styles = new WP_Styles(); 
  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. * Fires before styles in the $handles queue are printed. 
  45. * 
  46. * @since 2.6.0 
  47. */ 
  48. if ( ! $handles ) { 
  49. do_action( 'wp_print_styles' ); 
  50.  
  51. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 
  52.  
  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. return wp_styles()->do_items( $handles ); 
  59.  
  60. /** 
  61. * Add extra CSS styles to a registered stylesheet. 
  62. * 
  63. * Styles will only be added if the stylesheet in already in the queue. 
  64. * Accepts a string $data containing the CSS. If two or more CSS code blocks 
  65. * are added to the same stylesheet $handle, they will be printed in the order 
  66. * they were added, i.e. the latter added styles can redeclare the previous. 
  67. * 
  68. * @see WP_Styles::add_inline_style() 
  69. * 
  70. * @since 3.3.0 
  71. * 
  72. * @param string $handle Name of the stylesheet to add the extra styles to. 
  73. * @param string $data String containing the CSS styles to be added. 
  74. * @return bool True on success, false on failure. 
  75. */ 
  76. function wp_add_inline_style( $handle, $data ) { 
  77. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 
  78.  
  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><style></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.  
  114. return wp_styles()->add( $handle, $src, $deps, $ver, $media ); 
  115.  
  116. /** 
  117. * Remove a registered stylesheet. 
  118. * 
  119. * @see WP_Dependencies::remove() 
  120. * 
  121. * @since 2.1.0 
  122. * 
  123. * @param string $handle Name of the stylesheet to be removed. 
  124. */ 
  125. function wp_deregister_style( $handle ) { 
  126. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 
  127.  
  128. wp_styles()->remove( $handle ); 
  129.  
  130. /** 
  131. * Enqueue a CSS stylesheet. 
  132. * 
  133. * Registers the style if source provided (does NOT overwrite) and enqueues. 
  134. * 
  135. * @see WP_Dependencies::add() 
  136. * @see WP_Dependencies::enqueue() 
  137. * @link https://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types. 
  138. * 
  139. * @since 2.6.0 
  140. * 
  141. * @param string $handle Name of the stylesheet. Should be unique. 
  142. * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. 
  143. * Default empty. 
  144. * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array. 
  145. * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL 
  146. * as a query string for cache busting purposes. If version is set to false, a version 
  147. * number is automatically added equal to current installed WordPress version. 
  148. * If set to null, no version is added. 
  149. * @param string $media Optional. The media for which this stylesheet has been defined. 
  150. * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like 
  151. * '(orientation: portrait)' and '(max-width: 640px)'. 
  152. */ 
  153. function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) { 
  154. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 
  155.  
  156. $wp_styles = wp_styles(); 
  157.  
  158. if ( $src ) { 
  159. $_handle = explode('?', $handle); 
  160. $wp_styles->add( $_handle[0], $src, $deps, $ver, $media ); 
  161. $wp_styles->enqueue( $handle ); 
  162.  
  163. /** 
  164. * Remove a previously enqueued CSS stylesheet. 
  165. * 
  166. * @see WP_Dependencies::dequeue() 
  167. * 
  168. * @since 3.1.0 
  169. * 
  170. * @param string $handle Name of the stylesheet to be removed. 
  171. */ 
  172. function wp_dequeue_style( $handle ) { 
  173. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 
  174.  
  175. wp_styles()->dequeue( $handle ); 
  176.  
  177. /** 
  178. * Check whether a CSS stylesheet has been added to the queue. 
  179. * 
  180. * @since 2.8.0 
  181. * 
  182. * @param string $handle Name of the stylesheet. 
  183. * @param string $list Optional. Status of the stylesheet to check. Default 'enqueued'. 
  184. * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'. 
  185. * @return bool Whether style is queued. 
  186. */ 
  187. function wp_style_is( $handle, $list = 'enqueued' ) { 
  188. _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 
  189.  
  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 added. 
  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. * 
  204. * @see WP_Dependency::add_data() 
  205. * 
  206. * @since 3.6.0 
  207. * 
  208. * @param string $handle Name of the stylesheet. 
  209. * @param string $key Name of data point for which we're storing a value. 
  210. * Accepts 'conditional', 'rtl' and 'suffix', 'alt' and 'title'. 
  211. * @param mixed $value String containing the CSS data to be added. 
  212. * @return bool True on success, false on failure. 
  213. */ 
  214. function wp_style_add_data( $handle, $key, $value ) { 
  215. return wp_styles()->add_data( $handle, $key, $value ); 
.