[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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       * Holds a string which contains the type attribute for style tag.
 105       *
 106       * If the current theme does not declare HTML5 support for 'style',
 107       * then it initializes as `type='text/css'`.
 108       *
 109       * @since 5.3.0
 110       * @var string
 111       */
 112      private $type_attr = '';
 113  
 114      /**
 115       * Constructor.
 116       *
 117       * @since 2.6.0
 118       */
 119  	public function __construct() {
 120          if (
 121              function_exists( 'is_admin' ) && ! is_admin()
 122          &&
 123              function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'style' )
 124          ) {
 125              $this->type_attr = " type='text/css'";
 126          }
 127  
 128          /**
 129           * Fires when the WP_Styles instance is initialized.
 130           *
 131           * @since 2.6.0
 132           *
 133           * @param WP_Styles $this WP_Styles instance (passed by reference).
 134           */
 135          do_action_ref_array( 'wp_default_styles', array( &$this ) );
 136      }
 137  
 138      /**
 139       * Processes a style dependency.
 140       *
 141       * @since 2.6.0
 142       *
 143       * @see WP_Dependencies::do_item()
 144       *
 145       * @param string $handle The style's registered handle.
 146       * @return bool True on success, false on failure.
 147       */
 148  	public function do_item( $handle ) {
 149          if ( ! parent::do_item( $handle ) ) {
 150              return false;
 151          }
 152  
 153          $obj = $this->registered[ $handle ];
 154  
 155          if ( null === $obj->ver ) {
 156              $ver = '';
 157          } else {
 158              $ver = $obj->ver ? $obj->ver : $this->default_version;
 159          }
 160  
 161          if ( isset( $this->args[ $handle ] ) ) {
 162              $ver = $ver ? $ver . '&amp;' . $this->args[ $handle ] : $this->args[ $handle ];
 163          }
 164  
 165          $src         = $obj->src;
 166          $cond_before = '';
 167          $cond_after  = '';
 168          $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
 169  
 170          if ( $conditional ) {
 171              $cond_before = "<!--[if {$conditional}]>\n";
 172              $cond_after  = "<![endif]-->\n";
 173          }
 174  
 175          $inline_style = $this->print_inline_style( $handle, false );
 176  
 177          if ( $inline_style ) {
 178              $inline_style_tag = sprintf(
 179                  "<style id='%s-inline-css'%s>\n%s\n</style>\n",
 180                  esc_attr( $handle ),
 181                  $this->type_attr,
 182                  $inline_style
 183              );
 184          } else {
 185              $inline_style_tag = '';
 186          }
 187  
 188          if ( $this->do_concat ) {
 189              if ( $this->in_default_dir( $src ) && ! $conditional && ! isset( $obj->extra['alt'] ) ) {
 190                  $this->concat         .= "$handle,";
 191                  $this->concat_version .= "$handle$ver";
 192  
 193                  $this->print_code .= $inline_style;
 194  
 195                  return true;
 196              }
 197          }
 198  
 199          if ( isset( $obj->args ) ) {
 200              $media = esc_attr( $obj->args );
 201          } else {
 202              $media = 'all';
 203          }
 204  
 205          // A single item may alias a set of items, by having dependencies, but no source.
 206          if ( ! $src ) {
 207              if ( $inline_style_tag ) {
 208                  if ( $this->do_concat ) {
 209                      $this->print_html .= $inline_style_tag;
 210                  } else {
 211                      echo $inline_style_tag;
 212                  }
 213              }
 214  
 215              return true;
 216          }
 217  
 218          $href = $this->_css_href( $src, $ver, $handle );
 219          if ( ! $href ) {
 220              return true;
 221          }
 222  
 223          $rel   = isset( $obj->extra['alt'] ) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
 224          $title = isset( $obj->extra['title'] ) ? sprintf( "title='%s'", esc_attr( $obj->extra['title'] ) ) : '';
 225  
 226          $tag = sprintf(
 227              "<link rel='%s' id='%s-css' %s href='%s'%s media='%s' />\n",
 228              $rel,
 229              $handle,
 230              $title,
 231              $href,
 232              $this->type_attr,
 233              $media
 234          );
 235  
 236          /**
 237           * Filters the HTML link tag of an enqueued style.
 238           *
 239           * @since 2.6.0
 240           * @since 4.3.0 Introduced the `$href` parameter.
 241           * @since 4.5.0 Introduced the `$media` parameter.
 242           *
 243           * @param string $html   The link tag for the enqueued style.
 244           * @param string $handle The style's registered handle.
 245           * @param string $href   The stylesheet's source URL.
 246           * @param string $media  The stylesheet's media attribute.
 247           */
 248          $tag = apply_filters( 'style_loader_tag', $tag, $handle, $href, $media );
 249  
 250          if ( 'rtl' === $this->text_direction && isset( $obj->extra['rtl'] ) && $obj->extra['rtl'] ) {
 251              if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
 252                  $suffix   = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
 253                  $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $src, $ver, "$handle-rtl" ) );
 254              } else {
 255                  $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
 256              }
 257  
 258              $rtl_tag = sprintf(
 259                  "<link rel='%s' id='%s-rtl-css' %s href='%s'%s media='%s' />\n",
 260                  $rel,
 261                  $handle,
 262                  $title,
 263                  $rtl_href,
 264                  $this->type_attr,
 265                  $media
 266              );
 267  
 268              /** This filter is documented in wp-includes/class.wp-styles.php */
 269              $rtl_tag = apply_filters( 'style_loader_tag', $rtl_tag, $handle, $rtl_href, $media );
 270  
 271              if ( $obj->extra['rtl'] === 'replace' ) {
 272                  $tag = $rtl_tag;
 273              } else {
 274                  $tag .= $rtl_tag;
 275              }
 276          }
 277  
 278          if ( $this->do_concat ) {
 279              $this->print_html .= $cond_before;
 280              $this->print_html .= $tag;
 281              if ( $inline_style_tag ) {
 282                  $this->print_html .= $inline_style_tag;
 283              }
 284              $this->print_html .= $cond_after;
 285          } else {
 286              echo $cond_before;
 287              echo $tag;
 288              $this->print_inline_style( $handle );
 289              echo $cond_after;
 290          }
 291  
 292          return true;
 293      }
 294  
 295      /**
 296       * Adds extra CSS styles to a registered stylesheet.
 297       *
 298       * @since 3.3.0
 299       *
 300       * @param string $handle The style's registered handle.
 301       * @param string $code   String containing the CSS styles to be added.
 302       * @return bool True on success, false on failure.
 303       */
 304  	public function add_inline_style( $handle, $code ) {
 305          if ( ! $code ) {
 306              return false;
 307          }
 308  
 309          $after = $this->get_data( $handle, 'after' );
 310          if ( ! $after ) {
 311              $after = array();
 312          }
 313  
 314          $after[] = $code;
 315  
 316          return $this->add_data( $handle, 'after', $after );
 317      }
 318  
 319      /**
 320       * Prints extra CSS styles of a registered stylesheet.
 321       *
 322       * @since 3.3.0
 323       *
 324       * @param string $handle The style's registered handle.
 325       * @param bool   $echo   Optional. Whether to echo the inline style instead of just returning it.
 326       *                       Default true.
 327       * @return string|bool False if no data exists, inline styles if `$echo` is true, true otherwise.
 328       */
 329  	public function print_inline_style( $handle, $echo = true ) {
 330          $output = $this->get_data( $handle, 'after' );
 331  
 332          if ( empty( $output ) ) {
 333              return false;
 334          }
 335  
 336          $output = implode( "\n", $output );
 337  
 338          if ( ! $echo ) {
 339              return $output;
 340          }
 341  
 342          printf(
 343              "<style id='%s-inline-css'%s>\n%s\n</style>\n",
 344              esc_attr( $handle ),
 345              $this->type_attr,
 346              $output
 347          );
 348  
 349          return true;
 350      }
 351  
 352      /**
 353       * Determines style dependencies.
 354       *
 355       * @since 2.6.0
 356       *
 357       * @see WP_Dependencies::all_deps()
 358       *
 359       * @param mixed     $handles   Item handle and argument (string) or item handles and arguments (array of strings).
 360       * @param bool      $recursion Internal flag that function is calling itself.
 361       * @param int|false $group     Group level: (int) level, (false) no groups.
 362       * @return bool True on success, false on failure.
 363       */
 364  	public function all_deps( $handles, $recursion = false, $group = false ) {
 365          $r = parent::all_deps( $handles, $recursion, $group );
 366          if ( ! $recursion ) {
 367              /**
 368               * Filters the array of enqueued styles before processing for output.
 369               *
 370               * @since 2.6.0
 371               *
 372               * @param string[] $to_do The list of enqueued style handles about to be processed.
 373               */
 374              $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
 375          }
 376          return $r;
 377      }
 378  
 379      /**
 380       * Generates an enqueued style's fully-qualified URL.
 381       *
 382       * @since 2.6.0
 383       *
 384       * @param string $src The source of the enqueued style.
 385       * @param string $ver The version of the enqueued style.
 386       * @param string $handle The style's registered handle.
 387       * @return string Style's fully-qualified URL.
 388       */
 389  	public function _css_href( $src, $ver, $handle ) {
 390          if ( ! is_bool( $src ) && ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
 391              $src = $this->base_url . $src;
 392          }
 393  
 394          if ( ! empty( $ver ) ) {
 395              $src = add_query_arg( 'ver', $ver, $src );
 396          }
 397  
 398          /**
 399           * Filters an enqueued style's fully-qualified URL.
 400           *
 401           * @since 2.6.0
 402           *
 403           * @param string $src    The source URL of the enqueued style.
 404           * @param string $handle The style's registered handle.
 405           */
 406          $src = apply_filters( 'style_loader_src', $src, $handle );
 407          return esc_url( $src );
 408      }
 409  
 410      /**
 411       * Whether a handle's source is in a default directory.
 412       *
 413       * @since 2.8.0
 414       *
 415       * @param string $src The source of the enqueued style.
 416       * @return bool True if found, false if not.
 417       */
 418  	public function in_default_dir( $src ) {
 419          if ( ! $this->default_dirs ) {
 420              return true;
 421          }
 422  
 423          foreach ( (array) $this->default_dirs as $test ) {
 424              if ( 0 === strpos( $src, $test ) ) {
 425                  return true;
 426              }
 427          }
 428          return false;
 429      }
 430  
 431      /**
 432       * Processes items and dependencies for the footer group.
 433       *
 434       * HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
 435       *
 436       * @since 3.3.0
 437       *
 438       * @see WP_Dependencies::do_items()
 439       *
 440       * @return array Handles of items that have been processed.
 441       */
 442  	public function do_footer_items() {
 443          $this->do_items( false, 1 );
 444          return $this->done;
 445      }
 446  
 447      /**
 448       * Resets class properties.
 449       *
 450       * @since 3.3.0
 451       */
 452  	public function reset() {
 453          $this->do_concat      = false;
 454          $this->concat         = '';
 455          $this->concat_version = '';
 456          $this->print_html     = '';
 457      }
 458  }


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