[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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 active 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 $wp_styles 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       * @since 5.5.0 Added the `$group` parameter.
 143       *
 144       * @see WP_Dependencies::do_item()
 145       *
 146       * @param string    $handle The style's registered handle.
 147       * @param int|false $group  Optional. Group level: level (int), no groups (false).
 148       *                          Default false.
 149       * @return bool True on success, false on failure.
 150       */
 151  	public function do_item( $handle, $group = false ) {
 152          if ( ! parent::do_item( $handle ) ) {
 153              return false;
 154          }
 155  
 156          $obj = $this->registered[ $handle ];
 157          if ( $obj->extra['conditional'] ?? false ) {
 158  
 159              return false;
 160          }
 161          if ( null === $obj->ver ) {
 162              $ver = '';
 163          } else {
 164              $ver = $obj->ver ? $obj->ver : $this->default_version;
 165          }
 166  
 167          if ( isset( $this->args[ $handle ] ) ) {
 168              $ver = $ver ? $ver . '&amp;' . $this->args[ $handle ] : $this->args[ $handle ];
 169          }
 170  
 171          $src          = $obj->src;
 172          $inline_style = $this->print_inline_style( $handle, false );
 173  
 174          if ( $inline_style ) {
 175              $inline_style_tag = sprintf(
 176                  "<style id='%s-inline-css'%s>\n%s\n</style>\n",
 177                  esc_attr( $handle ),
 178                  $this->type_attr,
 179                  $inline_style
 180              );
 181          } else {
 182              $inline_style_tag = '';
 183          }
 184  
 185          if ( $this->do_concat ) {
 186              if ( $this->in_default_dir( $src ) && ! isset( $obj->extra['alt'] ) ) {
 187                  $this->concat         .= "$handle,";
 188                  $this->concat_version .= "$handle$ver";
 189  
 190                  $this->print_code .= $inline_style;
 191  
 192                  return true;
 193              }
 194          }
 195  
 196          if ( isset( $obj->args ) ) {
 197              $media = $obj->args;
 198          } else {
 199              $media = 'all';
 200          }
 201  
 202          // A single item may alias a set of items, by having dependencies, but no source.
 203          if ( ! $src ) {
 204              if ( $inline_style_tag ) {
 205                  if ( $this->do_concat ) {
 206                      $this->print_html .= $inline_style_tag;
 207                  } else {
 208                      echo $inline_style_tag;
 209                  }
 210              }
 211  
 212              return true;
 213          }
 214  
 215          $href = $this->_css_href( $src, $ver, $handle );
 216          if ( ! $href ) {
 217              return true;
 218          }
 219  
 220          $rel   = isset( $obj->extra['alt'] ) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
 221          $title = isset( $obj->extra['title'] ) ? $obj->extra['title'] : '';
 222  
 223          $tag = sprintf(
 224              "<link rel='%s' id='%s-css'%s href='%s'%s media='%s' />\n",
 225              $rel,
 226              esc_attr( $handle ),
 227              $title ? sprintf( " title='%s'", esc_attr( $title ) ) : '',
 228              $href,
 229              $this->type_attr,
 230              esc_attr( $media )
 231          );
 232  
 233          /**
 234           * Filters the HTML link tag of an enqueued style.
 235           *
 236           * @since 2.6.0
 237           * @since 4.3.0 Introduced the `$href` parameter.
 238           * @since 4.5.0 Introduced the `$media` parameter.
 239           *
 240           * @param string $tag    The link tag for the enqueued style.
 241           * @param string $handle The style's registered handle.
 242           * @param string $href   The stylesheet's source URL.
 243           * @param string $media  The stylesheet's media attribute.
 244           */
 245          $tag = apply_filters( 'style_loader_tag', $tag, $handle, $href, $media );
 246  
 247          if ( 'rtl' === $this->text_direction && isset( $obj->extra['rtl'] ) && $obj->extra['rtl'] ) {
 248              if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
 249                  $suffix   = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
 250                  $rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $src, $ver, "$handle-rtl" ) );
 251              } else {
 252                  $rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
 253              }
 254  
 255              $rtl_tag = sprintf(
 256                  "<link rel='%s' id='%s-rtl-css'%s href='%s'%s media='%s' />\n",
 257                  $rel,
 258                  esc_attr( $handle ),
 259                  $title ? sprintf( " title='%s'", esc_attr( $title ) ) : '',
 260                  $rtl_href,
 261                  $this->type_attr,
 262                  esc_attr( $media )
 263              );
 264  
 265              /** This filter is documented in wp-includes/class-wp-styles.php */
 266              $rtl_tag = apply_filters( 'style_loader_tag', $rtl_tag, $handle, $rtl_href, $media );
 267  
 268              if ( 'replace' === $obj->extra['rtl'] ) {
 269                  $tag = $rtl_tag;
 270              } else {
 271                  $tag .= $rtl_tag;
 272              }
 273          }
 274  
 275          if ( $this->do_concat ) {
 276              $this->print_html .= $tag;
 277              if ( $inline_style_tag ) {
 278                  $this->print_html .= $inline_style_tag;
 279              }
 280          } else {
 281              echo $tag;
 282              $this->print_inline_style( $handle );
 283          }
 284  
 285          return true;
 286      }
 287  
 288      /**
 289       * Adds extra CSS styles to a registered stylesheet.
 290       *
 291       * @since 3.3.0
 292       *
 293       * @param string $handle The style's registered handle.
 294       * @param string $code   String containing the CSS styles to be added.
 295       * @return bool True on success, false on failure.
 296       */
 297  	public function add_inline_style( $handle, $code ) {
 298          if ( ! $code ) {
 299              return false;
 300          }
 301  
 302          $after = $this->get_data( $handle, 'after' );
 303          if ( ! $after ) {
 304              $after = array();
 305          }
 306  
 307          $after[] = $code;
 308  
 309          return $this->add_data( $handle, 'after', $after );
 310      }
 311  
 312      /**
 313       * Prints extra CSS styles of a registered stylesheet.
 314       *
 315       * @since 3.3.0
 316       *
 317       * @param string $handle  The style's registered handle.
 318       * @param bool   $display Optional. Whether to print the inline style
 319       *                        instead of just returning it. Default true.
 320       * @return string|bool False if no data exists, inline styles if `$display` is true,
 321       *                     true otherwise.
 322       */
 323  	public function print_inline_style( $handle, $display = true ) {
 324          $output = $this->get_data( $handle, 'after' );
 325  
 326          if ( empty( $output ) || ! is_array( $output ) ) {
 327              return false;
 328          }
 329  
 330          if ( ! $this->do_concat ) {
 331  
 332              // Obtain the original `src` for a stylesheet possibly inlined by wp_maybe_inline_styles().
 333              $inlined_src = $this->get_data( $handle, 'inlined_src' );
 334  
 335              // If there's only one `after` inline style, and that inline style had been inlined, then use the $inlined_src
 336              // as the sourceURL. Otherwise, if there is more than one inline `after` style associated with the handle,
 337              // then resort to using the handle to construct the sourceURL since there isn't a single source.
 338              if ( count( $output ) === 1 && is_string( $inlined_src ) && strlen( $inlined_src ) > 0 ) {
 339                  $source_url = esc_url_raw( $inlined_src );
 340              } else {
 341                  $source_url = rawurlencode( "{$handle}-inline-css" );
 342              }
 343  
 344              $output[] = sprintf(
 345                  '/*# sourceURL=%s */',
 346                  $source_url
 347              );
 348          }
 349  
 350          $output = implode( "\n", $output );
 351  
 352          if ( ! $display ) {
 353              return $output;
 354          }
 355  
 356          printf(
 357              "<style id='%s-inline-css'%s>\n%s\n</style>\n",
 358              esc_attr( $handle ),
 359              $this->type_attr,
 360              $output
 361          );
 362  
 363          return true;
 364      }
 365  
 366      /**
 367       * Overrides the add_data method from WP_Dependencies, to allow unsetting dependencies for conditional styles.
 368       *
 369       * @since 6.9.0
 370       *
 371       * @param string $handle Name of the item. Should be unique.
 372       * @param string $key    The data key.
 373       * @param mixed  $value  The data value.
 374       * @return bool True on success, false on failure.
 375       */
 376  	public function add_data( $handle, $key, $value ) {
 377          if ( ! isset( $this->registered[ $handle ] ) ) {
 378              return false;
 379          }
 380  
 381          if ( 'conditional' === $key ) {
 382              $this->registered[ $handle ]->deps = array();
 383          }
 384  
 385          return parent::add_data( $handle, $key, $value );
 386      }
 387  
 388      /**
 389       * Determines style dependencies.
 390       *
 391       * @since 2.6.0
 392       *
 393       * @see WP_Dependencies::all_deps()
 394       *
 395       * @param string|string[] $handles   Item handle (string) or item handles (array of strings).
 396       * @param bool            $recursion Optional. Internal flag that function is calling itself.
 397       *                                   Default false.
 398       * @param int|false       $group     Optional. Group level: level (int), no groups (false).
 399       *                                   Default false.
 400       * @return bool True on success, false on failure.
 401       */
 402  	public function all_deps( $handles, $recursion = false, $group = false ) {
 403          $result = parent::all_deps( $handles, $recursion, $group );
 404          if ( ! $recursion ) {
 405              /**
 406               * Filters the array of enqueued styles before processing for output.
 407               *
 408               * @since 2.6.0
 409               *
 410               * @param string[] $to_do The list of enqueued style handles about to be processed.
 411               */
 412              $this->to_do = apply_filters( 'print_styles_array', $this->to_do );
 413          }
 414          return $result;
 415      }
 416  
 417      /**
 418       * Generates an enqueued style's fully-qualified URL.
 419       *
 420       * @since 2.6.0
 421       *
 422       * @param string $src    The source of the enqueued style.
 423       * @param string $ver    The version of the enqueued style.
 424       * @param string $handle The style's registered handle.
 425       * @return string Style's fully-qualified URL.
 426       */
 427  	public function _css_href( $src, $ver, $handle ) {
 428          if ( ! is_bool( $src ) && ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && str_starts_with( $src, $this->content_url ) ) ) {
 429              $src = $this->base_url . $src;
 430          }
 431  
 432          if ( ! empty( $ver ) ) {
 433              $src = add_query_arg( 'ver', $ver, $src );
 434          }
 435  
 436          /**
 437           * Filters an enqueued style's fully-qualified URL.
 438           *
 439           * @since 2.6.0
 440           *
 441           * @param string $src    The source URL of the enqueued style.
 442           * @param string $handle The style's registered handle.
 443           */
 444          $src = apply_filters( 'style_loader_src', $src, $handle );
 445          return esc_url( $src );
 446      }
 447  
 448      /**
 449       * Whether a handle's source is in a default directory.
 450       *
 451       * @since 2.8.0
 452       *
 453       * @param string $src The source of the enqueued style.
 454       * @return bool True if found, false if not.
 455       */
 456  	public function in_default_dir( $src ) {
 457          if ( ! $this->default_dirs ) {
 458              return true;
 459          }
 460  
 461          foreach ( (array) $this->default_dirs as $test ) {
 462              if ( str_starts_with( $src, $test ) ) {
 463                  return true;
 464              }
 465          }
 466          return false;
 467      }
 468  
 469      /**
 470       * Processes items and dependencies for the footer group.
 471       *
 472       * HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
 473       *
 474       * @since 3.3.0
 475       *
 476       * @see WP_Dependencies::do_items()
 477       *
 478       * @return string[] Handles of items that have been processed.
 479       */
 480  	public function do_footer_items() {
 481          $this->do_items( false, 1 );
 482          return $this->done;
 483      }
 484  
 485      /**
 486       * Resets class properties.
 487       *
 488       * @since 3.3.0
 489       */
 490  	public function reset() {
 491          $this->do_concat      = false;
 492          $this->concat         = '';
 493          $this->concat_version = '';
 494          $this->print_html     = '';
 495      }
 496  }


Generated : Thu Oct 30 08:20:06 2025 Cross-referenced by PHPXref