wp/wp-includes/class.wp-styles.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    15  * @package BackPress
    15  * @package BackPress
    16  * @uses WP_Dependencies
    16  * @uses WP_Dependencies
    17  * @since r74
    17  * @since r74
    18  */
    18  */
    19 class WP_Styles extends WP_Dependencies {
    19 class WP_Styles extends WP_Dependencies {
    20 	var $base_url;
    20 	public $base_url;
    21 	var $content_url;
    21 	public $content_url;
    22 	var $default_version;
    22 	public $default_version;
    23 	var $text_direction = 'ltr';
    23 	public $text_direction = 'ltr';
    24 	var $concat = '';
    24 	public $concat = '';
    25 	var $concat_version = '';
    25 	public $concat_version = '';
    26 	var $do_concat = false;
    26 	public $do_concat = false;
    27 	var $print_html = '';
    27 	public $print_html = '';
    28 	var $print_code = '';
    28 	public $print_code = '';
    29 	var $default_dirs;
    29 	public $default_dirs;
    30 
    30 
    31 	function __construct() {
    31 	public function __construct() {
       
    32 		/**
       
    33 		 * Fires when the WP_Styles instance is initialized.
       
    34 		 *
       
    35 		 * @since 2.6.0
       
    36 		 *
       
    37 		 * @param WP_Styles &$this WP_Styles instance, passed by reference.
       
    38 		 */
    32 		do_action_ref_array( 'wp_default_styles', array(&$this) );
    39 		do_action_ref_array( 'wp_default_styles', array(&$this) );
    33 	}
    40 	}
    34 
    41 
    35 	function do_item( $handle ) {
    42 	/**
       
    43 	 * @param string $handle
       
    44 	 * @return bool
       
    45 	 */
       
    46 	public function do_item( $handle ) {
    36 		if ( !parent::do_item($handle) )
    47 		if ( !parent::do_item($handle) )
    37 			return false;
    48 			return false;
    38 
    49 
    39 		$obj = $this->registered[$handle];
    50 		$obj = $this->registered[$handle];
    40 		if ( null === $obj->ver )
    51 		if ( null === $obj->ver )
    60 			$media = esc_attr( $obj->args );
    71 			$media = esc_attr( $obj->args );
    61 		else
    72 		else
    62 			$media = 'all';
    73 			$media = 'all';
    63 
    74 
    64 		$href = $this->_css_href( $obj->src, $ver, $handle );
    75 		$href = $this->_css_href( $obj->src, $ver, $handle );
       
    76 		if ( empty( $href ) ) {
       
    77 			// Turns out there is nothing to print.
       
    78 			return true;
       
    79 		}
    65 		$rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
    80 		$rel = isset($obj->extra['alt']) && $obj->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
    66 		$title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
    81 		$title = isset($obj->extra['title']) ? "title='" . esc_attr( $obj->extra['title'] ) . "'" : '';
    67 
    82 
    68 		$end_cond = $tag = '';
    83 		/**
    69 		if ( isset($obj->extra['conditional']) && $obj->extra['conditional'] ) {
    84 		 * Filter the HTML link tag of an enqueued style.
    70 			$tag .= "<!--[if {$obj->extra['conditional']}]>\n";
    85 		 *
    71 			$end_cond = "<![endif]-->\n";
    86 		 * @since 2.6.0
    72 		}
    87 		 *
    73 
    88 		 * @param string         The link tag for the enqueued style.
    74 		$tag .= apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle );
    89 		 * @param string $handle The style's registered handle.
       
    90 		 */
       
    91 		$tag = apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-css' $title href='$href' type='text/css' media='$media' />\n", $handle );
    75 		if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) {
    92 		if ( 'rtl' === $this->text_direction && isset($obj->extra['rtl']) && $obj->extra['rtl'] ) {
    76 			if ( is_bool( $obj->extra['rtl'] ) ) {
    93 			if ( is_bool( $obj->extra['rtl'] ) || 'replace' === $obj->extra['rtl'] ) {
    77 				$suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
    94 				$suffix = isset( $obj->extra['suffix'] ) ? $obj->extra['suffix'] : '';
    78 				$rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" ));
    95 				$rtl_href = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $this->_css_href( $obj->src , $ver, "$handle-rtl" ));
    79 			} else {
    96 			} else {
    80 				$rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
    97 				$rtl_href = $this->_css_href( $obj->extra['rtl'], $ver, "$handle-rtl" );
    81 			}
    98 			}
    82 
    99 
    83 			$tag .= apply_filters( 'style_loader_tag', "<link rel='$rel' id='$handle-rtl-css' $title href='$rtl_href' type='text/css' media='$media' />\n", $handle );
   100 			/** This filter is documented in wp-includes/class.wp-styles.php */
    84 		}
   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 );
    85 
   102 
    86 		$tag .= $end_cond;
   103 			if ( $obj->extra['rtl'] === 'replace' ) {
       
   104 				$tag = $rtl_tag;
       
   105 			} else {
       
   106 				$tag .= $rtl_tag;
       
   107 			}
       
   108 		}
       
   109 
       
   110 		$conditional_pre = $conditional_post = '';
       
   111 		if ( isset( $obj->extra['conditional'] ) && $obj->extra['conditional'] ) {
       
   112 			$conditional_pre  = "<!--[if {$obj->extra['conditional']}]>\n";
       
   113 			$conditional_post = "<![endif]-->\n";
       
   114 		}
    87 
   115 
    88 		if ( $this->do_concat ) {
   116 		if ( $this->do_concat ) {
       
   117 			$this->print_html .= $conditional_pre;
    89 			$this->print_html .= $tag;
   118 			$this->print_html .= $tag;
    90 			if ( $inline_style = $this->print_inline_style( $handle, false ) )
   119 			if ( $inline_style = $this->print_inline_style( $handle, false ) ) {
    91 				$this->print_html .= sprintf( "<style type='text/css'>\n%s\n</style>\n", $inline_style );
   120 				$this->print_html .= sprintf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $inline_style );
       
   121 			}
       
   122 			$this->print_html .= $conditional_post;
    92 		} else {
   123 		} else {
       
   124 			echo $conditional_pre;
    93 			echo $tag;
   125 			echo $tag;
    94 			$this->print_inline_style( $handle );
   126 			$this->print_inline_style( $handle );
       
   127 			echo $conditional_post;
    95 		}
   128 		}
    96 
   129 
    97 		return true;
   130 		return true;
    98 	}
   131 	}
    99 
   132 
   100 	function add_inline_style( $handle, $code ) {
   133 	/**
   101 		if ( !$code )
   134 	 * @param string $handle
       
   135 	 * @param string $code
       
   136 	 */
       
   137 	public function add_inline_style( $handle, $code ) {
       
   138 		if ( ! $code ) {
   102 			return false;
   139 			return false;
       
   140 		}
   103 
   141 
   104 		$after = $this->get_data( $handle, 'after' );
   142 		$after = $this->get_data( $handle, 'after' );
   105 		if ( !$after )
   143 		if ( ! $after ) {
   106 			$after = array();
   144 			$after = array();
       
   145 		}
   107 
   146 
   108 		$after[] = $code;
   147 		$after[] = $code;
   109 
   148 
   110 		return $this->add_data( $handle, 'after', $after );
   149 		return $this->add_data( $handle, 'after', $after );
   111 	}
   150 	}
   112 
   151 
   113 	function print_inline_style( $handle, $echo = true ) {
   152 	/**
       
   153 	 * @param string $handle
       
   154 	 * @param bool $echo
       
   155 	 * @return bool
       
   156 	 */
       
   157 	public function print_inline_style( $handle, $echo = true ) {
   114 		$output = $this->get_data( $handle, 'after' );
   158 		$output = $this->get_data( $handle, 'after' );
   115 
   159 
   116 		if ( empty( $output ) )
   160 		if ( empty( $output ) ) {
   117 			return false;
   161 			return false;
       
   162 		}
   118 
   163 
   119 		$output = implode( "\n", $output );
   164 		$output = implode( "\n", $output );
   120 
   165 
   121 		if ( !$echo )
   166 		if ( ! $echo ) {
   122 			return $output;
   167 			return $output;
   123 
   168 		}
   124 		echo "<style type='text/css'>\n";
   169 
   125 		echo "$output\n";
   170 		printf( "<style id='%s-inline-css' type='text/css'>\n%s\n</style>\n", esc_attr( $handle ), $output );
   126 		echo "</style>\n";
       
   127 
   171 
   128 		return true;
   172 		return true;
   129 	}
   173 	}
   130 
   174 
   131 	function all_deps( $handles, $recursion = false, $group = false ) {
   175 	/**
       
   176 	 * @param mixed $handles
       
   177 	 * @param bool $recursion
       
   178 	 * @param mixed $group
       
   179 	 * @return bool
       
   180 	 */
       
   181 	public function all_deps( $handles, $recursion = false, $group = false ) {
   132 		$r = parent::all_deps( $handles, $recursion );
   182 		$r = parent::all_deps( $handles, $recursion );
   133 		if ( !$recursion )
   183 		if ( !$recursion ) {
       
   184 			/**
       
   185 			 * Filter the array of enqueued styles before processing for output.
       
   186 			 *
       
   187 			 * @since 2.6.0
       
   188 			 *
       
   189 			 * @param array $to_do The list of enqueued styles about to be processed.
       
   190 			 */
   134 			$this->to_do = apply_filters( 'print_styles_array', $this->to_do );
   191 			$this->to_do = apply_filters( 'print_styles_array', $this->to_do );
       
   192 		}
   135 		return $r;
   193 		return $r;
   136 	}
   194 	}
   137 
   195 
   138 	function _css_href( $src, $ver, $handle ) {
   196 	/**
       
   197 	 * @param string $src
       
   198 	 * @param string $ver
       
   199 	 * @param string $handle
       
   200 	 * @return string
       
   201 	 */
       
   202 	public function _css_href( $src, $ver, $handle ) {
   139 		if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
   203 		if ( !is_bool($src) && !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
   140 			$src = $this->base_url . $src;
   204 			$src = $this->base_url . $src;
   141 		}
   205 		}
   142 
   206 
   143 		if ( !empty($ver) )
   207 		if ( !empty($ver) )
   144 			$src = add_query_arg('ver', $ver, $src);
   208 			$src = add_query_arg('ver', $ver, $src);
       
   209 
       
   210 		/**
       
   211 		 * Filter an enqueued style's fully-qualified URL.
       
   212 		 *
       
   213 		 * @since 2.6.0
       
   214 		 *
       
   215 		 * @param string $src    The source URL of the enqueued style.
       
   216 		 * @param string $handle The style's registered handle.
       
   217 		 */
   145 		$src = apply_filters( 'style_loader_src', $src, $handle );
   218 		$src = apply_filters( 'style_loader_src', $src, $handle );
   146 		return esc_url( $src );
   219 		return esc_url( $src );
   147 	}
   220 	}
   148 
   221 
   149 	function in_default_dir($src) {
   222 	/**
       
   223 	 * @param string $src
       
   224 	 * @return bool
       
   225 	 */
       
   226 	public function in_default_dir($src) {
   150 		if ( ! $this->default_dirs )
   227 		if ( ! $this->default_dirs )
   151 			return true;
   228 			return true;
   152 
   229 
   153 		foreach ( (array) $this->default_dirs as $test ) {
   230 		foreach ( (array) $this->default_dirs as $test ) {
   154 			if ( 0 === strpos($src, $test) )
   231 			if ( 0 === strpos($src, $test) )
   155 				return true;
   232 				return true;
   156 		}
   233 		}
   157 		return false;
   234 		return false;
   158 	}
   235 	}
   159 
   236 
   160 	function do_footer_items() { // HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
   237 	public function do_footer_items() { // HTML 5 allows styles in the body, grab late enqueued items and output them in the footer.
   161 		$this->do_items(false, 1);
   238 		$this->do_items(false, 1);
   162 		return $this->done;
   239 		return $this->done;
   163 	}
   240 	}
   164 
   241 
   165 	function reset() {
   242 	public function reset() {
   166 		$this->do_concat = false;
   243 		$this->do_concat = false;
   167 		$this->concat = '';
   244 		$this->concat = '';
   168 		$this->concat_version = '';
   245 		$this->concat_version = '';
   169 		$this->print_html = '';
   246 		$this->print_html = '';
   170 	}
   247 	}