[ Index ]

PHP Cross Reference of WordPress

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


Generated: Tue Dec 10 01:00:04 2019 Cross-referenced by PHPXref 0.7.1