[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Dependencies API: WP_Styles class
   4   *
   5   * @since 2.6.0
   6   *
   7   * @package WordPress
   8   * @subpackage Dependencies
   9   */
  10  
  11  /**
  12   * Core class used to register styles.
  13   *
  14   * @since 2.6.0
  15   *
  16   * @see WP_Dependencies
  17   */
  18  class WP_Styles extends WP_Dependencies {
  19      /**
  20       * Base URL for styles.
  21       *
  22       * Full URL with trailing slash.
  23       *
  24       * @since 2.6.0
  25       * @var string
  26       */
  27      public $base_url;
  28  
  29      /**
  30       * URL of the content directory.
  31       *
  32       * @since 2.8.0
  33       * @var string
  34       */
  35      public $content_url;
  36  
  37      /**
  38       * Default version string for stylesheets.
  39       *
  40       * @since 2.6.0
  41       * @var string
  42       */
  43      public $default_version;
  44  
  45      /**
  46       * The current text direction.
  47       *
  48       * @since 2.6.0
  49       * @var string
  50       */
  51      public $text_direction = 'ltr';
  52  
  53      /**
  54       * Holds a list of style handles which will be concatenated.
  55       *
  56       * @since 2.8.0
  57       * @var string
  58       */
  59      public $concat = '';
  60  
  61      /**
  62       * Holds a string which contains style handles and their version.
  63       *
  64       * @since 2.8.0
  65       * @deprecated 3.4.0
  66       * @var string
  67       */
  68      public $concat_version = '';
  69  
  70      /**
  71       * Whether to perform concatenation.
  72       *
  73       * @since 2.8.0
  74       * @var bool
  75       */
  76      public $do_concat = false;
  77  
  78      /**
  79       * Holds HTML markup of styles and additional data if concatenation
  80       * is enabled.
  81       *
  82       * @since 2.8.0
  83       * @var string
  84       */
  85      public $print_html = '';
  86  
  87      /**
  88       * Holds inline styles if concatenation is enabled.
  89       *
  90       * @since 3.3.0
  91       * @var string
  92       */
  93      public $print_code = '';
  94  
  95      /**
  96       * List of default directories.
  97       *
  98       * @since 2.8.0
  99       * @var array
 100       */
 101      public $default_dirs;
 102  
 103      /**
 104       * Constructor.
 105       *
 106       * @since 2.6.0
 107       */
 108  	public function __construct() {
 109          /**
 110           * Fires when the WP_Styles instance is initialized.
 111           *
 112           * @since 2.6.0
 113           *
 114           * @param WP_Styles $this WP_Styles instance (passed by reference).
 115           */
 116          do_action_ref_array( 'wp_default_styles', array( &$this ) );
 117      }
 118  
 119      /**
 120       * Processes a style dependency.
 121       *
 122       * @since 2.6.0
 123       *
 124       * @see WP_Dependencies::do_item()
 125       *
 126       * @param string $handle The style's registered handle.
 127       * @return bool True on success, false on failure.
 128       */
 129  	public function do_item( $handle ) {
 130          if ( ! parent::do_item( $handle ) ) {
 131              return false;
 132          }
 133  
 134          $obj = $this->registered[ $handle ];
 135  
 136          if ( null === $obj->ver ) {
 137              $ver = '';
 138          } else {
 139              $ver = $obj->ver ? $obj->ver : $this->default_version;
 140          }
 141  
 142          if ( isset( $this->args[ $handle ] ) ) {
 143              $ver = $ver ? $ver . '&amp;' . $this->args[ $handle ] : $this->args[ $handle ];
 144          }
 145  
 146          $src         = $obj->src;
 147          $cond_before = '';
 148          $cond_after  = '';
 149          $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
 150  
 151          if ( $conditional ) {
 152              $cond_before = "<!--[if {$conditional}]>\n";
 153              $cond_after  = "<![endif]-->\n";
 154          }
 155  
 156          $inline_style = $this->print_inline_style( $handle, false );
 157  
 158          if ( $inline_style ) {
 159              $inline_style_tag = sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style );
 160          } else {
 161              $inline_style_tag = '';
 162          }
 163  
 164          if ( $this->do_concat ) {
 165              if ( $this->in_default_dir( $src ) && ! $conditional && ! isset( $obj->extra['alt'] ) ) {
 166                  $this->concat         .= "$handle,";
 167                  $this->concat_version .= "$handle$ver";
 168  
 169                  $this->print_code .= $inline_style;
 170  
 171                  return true;
 172              }
 173          }
 174  
 175          if ( isset( $obj->args ) ) {
 176              $media = esc_attr( $obj->args );
 177          } else {
 178              $media = 'all';
 179          }
 180  
 181          // A single item may alias a set of items, by having dependencies, but no source.
 182          if ( ! $src ) {
 183              if ( $inline_style_tag ) {
 184                  if ( $this->do_concat ) {
 185                      $this->print_html .= $inline_style_tag;
 186                  } else {
 187                      echo $inline_style_tag;
 188                  }
 189              }
 190  
 191              return true;
 192          }
 193  
 194          $href = $this->_css_href( $src, $ver, $handle );
 195          if ( ! $href ) {
 196              return true;
 197          }
 198  
 199          $rel   = isset( $obj->extra['alt'] ) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
 200          $title = isset( $obj->extra['title'] ) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
 201  
 202          $tag = "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n";
 203  
 204          /**
 205           * Filters the HTML link tag of an enqueued style.
 206           *
 207           * @since 2.6.0
 208           * @since 4.3.0 Introduced the `$href` parameter.
 209           * @since 4.5.0 Introduced the `$media` parameter.
 210           *
 211           * @param string $html   The link tag for the enqueued style.
 212           * @param string $handle The style's registered handle.
 213           * @param string $href   The stylesheet's source URL.
 214           * @param string $media  The stylesheet's media attribute.
 215           */
 216          $tag = apply_filters( 'style_loader_tag', $tag, $handle, $href, $media );
 217  
 218          if ( 'rtl' === $this->text_direction && isset( $obj->extra['rtl'] ) && $obj->extra['rtl'] ) {
 219              if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
 220                  $suffix   = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
 221                  $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $src, $ver, "$handle-rtl" ) );
 222              } else {
 223                  $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
 224              }
 225  
 226              $rtl_tag = "<link rel='$rel' id='$handle-rtl-css' $title href='$rtl_href' type='text/css' media='$media' />\n";
 227              /** This filter is documented in wp-includes/class.wp-styles.php */
 228              $rtl_tag = apply_filters( 'style_loader_tag', $rtl_tag, $handle, $rtl_href, $media );
 229  
 230              if ( $obj->extra['rtl'] === 'replace' ) {
 231                  $tag = $rtl_tag;
 232              } else {
 233                  $tag .= $rtl_tag;
 234              }
 235          }
 236  
 237          if ( $this->do_concat ) {
 238              $this->print_html .= $cond_before;
 239              $this->print_html .= $tag;
 240              if ( $inline_style_tag ) {
 241                  $this->print_html .= $inline_style_tag;
 242              }
 243              $this->print_html .= $cond_after;
 244          } else {
 245              echo $cond_before;
 246              echo $tag;
 247              $this->print_inline_style( $handle );
 248              echo $cond_after;
 249          }
 250  
 251          return true;
 252      }
 253  
 254      /**
 255       * Adds extra CSS styles to a registered stylesheet.
 256       *
 257       * @since 3.3.0
 258       *
 259       * @param string $handle The style's registered handle.
 260       * @param string $code   String containing the CSS styles to be added.
 261       * @return bool True on success, false on failure.
 262       */
 263  	public function add_inline_style( $handle, $code ) {
 264          if ( ! $code ) {
 265              return false;
 266          }
 267  
 268          $after = $this->get_data( $handle, 'after' );
 269          if ( ! $after ) {
 270              $after = array();
 271          }
 272  
 273          $after[] = $code;
 274  
 275          return $this->add_data( $handle, 'after', $after );
 276      }
 277  
 278      /**
 279       * Prints extra CSS styles of a registered stylesheet.
 280       *
 281       * @since 3.3.0
 282       *
 283       * @param string $handle The style's registered handle.
 284       * @param bool   $echo   Optional. Whether to echo the inline style instead of just returning it.
 285       *                       Default true.
 286       * @return string|bool False if no data exists, inline styles if `$echo` is true, true otherwise.
 287       */
 288  	public function print_inline_style( $handle, $echo = true ) {
 289          $output = $this->get_data( $handle, 'after' );
 290  
 291          if ( empty( $output ) ) {
 292              return false;
 293          }
 294  
 295          $output = implode( "\n", $output );
 296  
 297          if ( ! $echo ) {
 298              return $output;
 299          }
 300  
 301          printf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $output );
 302  
 303          return true;
 304      }
 305  
 306      /**
 307       * Determines style dependencies.
 308       *
 309       * @since 2.6.0
 310       *
 311       * @see WP_Dependencies::all_deps()
 312       *
 313       * @param mixed     $handles   Item handle and argument (string) or item handles and arguments (array of strings).
 314       * @param bool      $recursion Internal flag that function is calling itself.
 315       * @param int|false $group     Group level: (int) level, (false) no groups.
 316       * @return bool True on success, false on failure.
 317       */
 318  	public function all_deps( $handles, $recursion = false, $group = false ) {
 319          $r = parent::all_deps( $handles, $recursion, $group );
 320          if ( ! $recursion ) {
 321              /**
 322               * Filters the array of enqueued styles before processing for output.
 323               *
 324               * @since 2.6.0
 325               *
 326               * @param string[] $to_do The list of enqueued style handles about to be processed.
 327               */
 328              $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
 329          }
 330          return $r;
 331      }
 332  
 333      /**
 334       * Generates an enqueued style's fully-qualified URL.
 335       *
 336       * @since 2.6.0
 337       *
 338       * @param string $src The source of the enqueued style.
 339       * @param string $ver The version of the enqueued style.
 340       * @param string $handle The style's registered handle.
 341       * @return string Style's fully-qualified URL.
 342       */
 343  	public function _css_href( $src, $ver, $handle ) {
 344          if ( ! is_bool( $src ) && ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
 345              $src = $this->base_url . $src;
 346          }
 347  
 348          if ( ! empty( $ver ) ) {
 349              $src = add_query_arg( 'ver', $ver, $src );
 350          }
 351  
 352          /**
 353           * Filters an enqueued style's fully-qualified URL.
 354           *
 355           * @since 2.6.0
 356           *
 357           * @param string $src    The source URL of the enqueued style.
 358           * @param string $handle The style's registered handle.
 359           */
 360          $src = apply_filters( 'style_loader_src', $src, $handle );
 361          return esc_url( $src );
 362      }
 363  
 364      /**
 365       * Whether a handle's source is in a default directory.
 366       *
 367       * @since 2.8.0
 368       *
 369       * @param string $src The source of the enqueued style.
 370       * @return bool True if found, false if not.
 371       */
 372  	public function in_default_dir( $src ) {
 373          if ( ! $this->default_dirs ) {
 374              return true;
 375          }
 376  
 377          foreach ( (array) $this->default_dirs as $test ) {
 378              if ( 0 === strpos( $src, $test ) ) {
 379                  return true;
 380              }
 381          }
 382          return false;
 383      }
 384  
 385      /**
 386       * Processes items and dependencies for the footer group.
 387       *
 388       * HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
 389       *
 390       * @since 3.3.0
 391       *
 392       * @see WP_Dependencies::do_items()
 393       *
 394       * @return array Handles of items that have been processed.
 395       */
 396  	public function do_footer_items() {
 397          $this->do_items( false, 1 );
 398          return $this->done;
 399      }
 400  
 401      /**
 402       * Resets class properties.
 403       *
 404       * @since 3.3.0
 405       */
 406  	public function reset() {
 407          $this->do_concat      = false;
 408          $this->concat         = '';
 409          $this->concat_version = '';
 410          $this->print_html     = '';
 411      }
 412  }


Generated: Sun Sep 15 01:00:03 2019 Cross-referenced by PHPXref 0.7.1