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