web/wp-includes/functions.wp-styles.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    11  *
    11  *
    12  * @since r79
    12  * @since r79
    13  * @uses do_action() Calls 'wp_print_styles' hook.
    13  * @uses do_action() Calls 'wp_print_styles' hook.
    14  * @global object $wp_styles The WP_Styles object for printing styles.
    14  * @global object $wp_styles The WP_Styles object for printing styles.
    15  *
    15  *
    16  * @param array $handles (optional) Styles to be printed.  (void) prints queue, (string) prints that style, (array of strings) prints those styles.
    16  * @param array|bool $handles Styles to be printed. An empty array prints the queue,
       
    17  *  an array with one string prints that style, and an array of strings prints those styles.
    17  * @return bool True on success, false on failure.
    18  * @return bool True on success, false on failure.
    18  */
    19  */
    19 function wp_print_styles( $handles = false ) {
    20 function wp_print_styles( $handles = false ) {
    20 	do_action( 'wp_print_styles' );
       
    21 	if ( '' === $handles ) // for wp_head
    21 	if ( '' === $handles ) // for wp_head
    22 		$handles = false;
    22 		$handles = false;
    23 
    23 
       
    24 	if ( ! $handles )
       
    25 		do_action( 'wp_print_styles' );
       
    26 
    24 	global $wp_styles;
    27 	global $wp_styles;
    25 	if ( !is_a($wp_styles, 'WP_Styles') ) {
    28 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
       
    29 		if ( ! did_action( 'init' ) )
       
    30 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
    31 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
       
    32 
    26 		if ( !$handles )
    33 		if ( !$handles )
    27 			return array(); // No need to instantiate if nothing's there.
    34 			return array(); // No need to instantiate if nothing is there.
    28 		else
    35 		else
    29 			$wp_styles = new WP_Styles();
    36 			$wp_styles = new WP_Styles();
    30 	}
    37 	}
    31 
    38 
    32 	return $wp_styles->do_items( $handles );
    39 	return $wp_styles->do_items( $handles );
    33 }
    40 }
    34 
    41 
    35 /**
    42 /**
       
    43  * Adds extra CSS.
       
    44  *
       
    45  * Works only if the stylesheet has already been added.
       
    46  * Accepts a string $data containing the CSS. If two or more CSS code blocks are
       
    47  * added to the same stylesheet $handle, they will be printed in the order
       
    48  * they were added, i.e. the latter added styles can redeclare the previous.
       
    49  *
       
    50  * @since 3.3.0
       
    51  * @see WP_Scripts::add_inline_style()
       
    52  */
       
    53 function wp_add_inline_style( $handle, $data ) {
       
    54 	global $wp_styles;
       
    55 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
       
    56 		if ( ! did_action( 'init' ) )
       
    57 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
    58 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
       
    59 		$wp_styles = new WP_Styles();
       
    60 	}
       
    61 
       
    62 	return $wp_styles->add_inline_style( $handle, $data );
       
    63 }
       
    64 
       
    65 /**
    36  * Register CSS style file.
    66  * Register CSS style file.
    37  *
    67  *
    38  * @since r79
    68  * @since r79
    39  * @see WP_Styles::add() For parameter and additional information.
    69  * @see WP_Styles::add() For additional information.
       
    70  * @global object $wp_styles The WP_Styles object for printing styles.
       
    71  * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
       
    72  *
       
    73  * @param string $handle Name of the stylesheet.
       
    74  * @param string|bool $src Path to the stylesheet from the root directory of WordPress. Example: '/css/mystyle.css'.
       
    75  * @param array $deps Array of handles of any stylesheet that this stylesheet depends on.
       
    76  *  (Stylesheets that must be loaded before this stylesheet.) Pass an empty array if there are no dependencies.
       
    77  * @param string|bool $ver String specifying the stylesheet version number. Set to null to disable.
       
    78  *  Used to ensure that the correct version is sent to the client regardless of caching.
       
    79  * @param string $media The media for which this stylesheet has been defined.
    40  */
    80  */
    41 function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
    81 function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
    42 	global $wp_styles;
    82 	global $wp_styles;
    43 	if ( !is_a($wp_styles, 'WP_Styles') )
    83 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
       
    84 		if ( ! did_action( 'init' ) )
       
    85 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
    86 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
    44 		$wp_styles = new WP_Styles();
    87 		$wp_styles = new WP_Styles();
       
    88 	}
    45 
    89 
    46 	$wp_styles->add( $handle, $src, $deps, $ver, $media );
    90 	$wp_styles->add( $handle, $src, $deps, $ver, $media );
    47 }
    91 }
    48 
    92 
    49 /**
    93 /**
    50  * Remove a registered CSS file.
    94  * Remove a registered CSS file.
    51  *
    95  *
    52  * @since r79
    96  * @since r79
    53  * @see WP_Styles::remove() For parameter and additional information.
    97  * @see WP_Styles::remove() For additional information.
       
    98  * @global object $wp_styles The WP_Styles object for printing styles.
       
    99  *
       
   100  * @param string $handle Name of the stylesheet.
    54  */
   101  */
    55 function wp_deregister_style( $handle ) {
   102 function wp_deregister_style( $handle ) {
    56 	global $wp_styles;
   103 	global $wp_styles;
    57 	if ( !is_a($wp_styles, 'WP_Styles') )
   104 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
       
   105 		if ( ! did_action( 'init' ) )
       
   106 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   107 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
    58 		$wp_styles = new WP_Styles();
   108 		$wp_styles = new WP_Styles();
       
   109 	}
    59 
   110 
    60 	$wp_styles->remove( $handle );
   111 	$wp_styles->remove( $handle );
    61 }
   112 }
    62 
   113 
    63 /**
   114 /**
    64  * Enqueue a CSS style file.
   115  * Enqueue a CSS style file.
    65  *
   116  *
       
   117  * Registers the style if src provided (does NOT overwrite) and enqueues.
       
   118  *
    66  * @since r79
   119  * @since r79
    67  * @see WP_Styles::add(), WP_Styles::enqueue()
   120  * @see WP_Styles::add(), WP_Styles::enqueue()
       
   121  * @global object $wp_styles The WP_Styles object for printing styles.
       
   122  * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
       
   123  *
       
   124  * @param string $handle Name of the stylesheet.
       
   125  * @param string|bool $src Path to the stylesheet from the root directory of WordPress. Example: '/css/mystyle.css'.
       
   126  * @param array $deps Array of handles (names) of any stylesheet that this stylesheet depends on.
       
   127  *  (Stylesheets that must be loaded before this stylesheet.) Pass an empty array if there are no dependencies.
       
   128  * @param string|bool $ver String specifying the stylesheet version number, if it has one. This parameter
       
   129  *  is used to ensure that the correct version is sent to the client regardless of caching, and so should be included
       
   130  *  if a version number is available and makes sense for the stylesheet.
       
   131  * @param string $media The media for which this stylesheet has been defined.
    68  */
   132  */
    69 function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = false ) {
   133 function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
    70 	global $wp_styles;
   134 	global $wp_styles;
    71 	if ( !is_a($wp_styles, 'WP_Styles') )
   135 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
       
   136 		if ( ! did_action( 'init' ) )
       
   137 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   138 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
    72 		$wp_styles = new WP_Styles();
   139 		$wp_styles = new WP_Styles();
       
   140 	}
    73 
   141 
    74 	if ( $src ) {
   142 	if ( $src ) {
    75 		$_handle = explode('?', $handle);
   143 		$_handle = explode('?', $handle);
    76 		$wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
   144 		$wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
    77 	}
   145 	}
    78 	$wp_styles->enqueue( $handle );
   146 	$wp_styles->enqueue( $handle );
    79 }
   147 }
    80 
   148 
    81 /**
   149 /**
       
   150  * Remove an enqueued style.
       
   151  *
       
   152  * @since WP 3.1
       
   153  * @see WP_Styles::dequeue() For parameter information.
       
   154  */
       
   155 function wp_dequeue_style( $handle ) {
       
   156 	global $wp_styles;
       
   157 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
       
   158 		if ( ! did_action( 'init' ) )
       
   159 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   160 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
       
   161 		$wp_styles = new WP_Styles();
       
   162 	}
       
   163 
       
   164 	$wp_styles->dequeue( $handle );
       
   165 }
       
   166 
       
   167 /**
    82  * Check whether style has been added to WordPress Styles.
   168  * Check whether style has been added to WordPress Styles.
    83  *
   169  *
    84  * The values for list defaults to 'queue', which is the same as enqueue for
   170  * The values for list defaults to 'queue', which is the same as wp_enqueue_style().
    85  * styles.
       
    86  *
   171  *
    87  * @since WP unknown; BP unknown
   172  * @since WP unknown; BP unknown
       
   173  * @global object $wp_styles The WP_Styles object for printing styles.
    88  *
   174  *
    89  * @param string $handle Handle used to add style.
   175  * @param string $handle Name of the stylesheet.
    90  * @param string $list Optional, defaults to 'queue'. Others values are 'registered', 'queue', 'done', 'to_do'
   176  * @param string $list Values are 'registered', 'done', 'queue' and 'to_do'.
    91  * @return bool
   177  * @return bool True on success, false on failure.
    92  */
   178  */
    93 function wp_style_is( $handle, $list = 'queue' ) {
   179 function wp_style_is( $handle, $list = 'queue' ) {
    94 	global $wp_styles;
   180 	global $wp_styles;
    95 	if ( !is_a($wp_styles, 'WP_Styles') )
   181 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
       
   182 		if ( ! did_action( 'init' ) )
       
   183 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   184 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
    96 		$wp_styles = new WP_Styles();
   185 		$wp_styles = new WP_Styles();
       
   186 	}
    97 
   187 
    98 	$query = $wp_styles->query( $handle, $list );
   188 	$query = $wp_styles->query( $handle, $list );
    99 
   189 
   100 	if ( is_object( $query ) )
   190 	if ( is_object( $query ) )
   101 		return true;
   191 		return true;