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


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1