[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> functions.wp-styles.php (source)

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


Generated: Fri Oct 25 08:20:01 2019 Cross-referenced by PHPXref 0.7