wp/wp-includes/functions.wp-styles.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     5  * @since 2.6.0
     5  * @since 2.6.0
     6  *
     6  *
     7  * @package WordPress
     7  * @package WordPress
     8  * @subpackage BackPress
     8  * @subpackage BackPress
     9  */
     9  */
       
    10 
       
    11 /**
       
    12  * Initialize $wp_styles if it has not been set.
       
    13  *
       
    14  * @global WP_Styles $wp_styles
       
    15  *
       
    16  * @since 4.2.0
       
    17  *
       
    18  * @return WP_Styles WP_Styles instance.
       
    19  */
       
    20 function wp_styles() {
       
    21 	global $wp_styles;
       
    22 	if ( ! ( $wp_styles instanceof WP_Styles ) ) {
       
    23 		$wp_styles = new WP_Styles();
       
    24 	}
       
    25 	return $wp_styles;
       
    26 }
    10 
    27 
    11 /**
    28 /**
    12  * Display styles that are in the $handles queue.
    29  * Display styles that are in the $handles queue.
    13  *
    30  *
    14  * Passing an empty array to $handles prints the queue,
    31  * Passing an empty array to $handles prints the queue,
    15  * passing an array with one string prints that style,
    32  * passing an array with one string prints that style,
    16  * and passing an array of strings prints those styles.
    33  * and passing an array of strings prints those styles.
    17  *
    34  *
    18  * @see do_action() Calls 'wp_print_styles' hook.
       
    19  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
    35  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
    20  *
    36  *
    21  * @since 2.6.0
    37  * @since 2.6.0
    22  *
    38  *
    23  * @param array|bool $handles Styles to be printed. Default 'false'.
    39  * @param string|bool|array $handles Styles to be printed. Default 'false'.
    24  * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
    40  * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
    25  */
    41  */
    26 function wp_print_styles( $handles = false ) {
    42 function wp_print_styles( $handles = false ) {
    27 	if ( '' === $handles ) // for wp_head
    43 	if ( '' === $handles ) { // for wp_head
    28 		$handles = false;
    44 		$handles = false;
    29 
    45 	}
    30 	if ( ! $handles )
    46 	/**
       
    47 	 * Fires before styles in the $handles queue are printed.
       
    48 	 *
       
    49 	 * @since 2.6.0
       
    50 	 */
       
    51 	if ( ! $handles ) {
    31 		do_action( 'wp_print_styles' );
    52 		do_action( 'wp_print_styles' );
       
    53 	}
       
    54 
       
    55 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    32 
    56 
    33 	global $wp_styles;
    57 	global $wp_styles;
    34 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
    58 	if ( ! ( $wp_styles instanceof WP_Styles ) ) {
    35 		if ( ! did_action( 'init' ) )
    59 		if ( ! $handles ) {
    36 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
    37 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
       
    38 
       
    39 		if ( !$handles )
       
    40 			return array(); // No need to instantiate if nothing is there.
    60 			return array(); // No need to instantiate if nothing is there.
    41 		else
    61 		}
    42 			$wp_styles = new WP_Styles();
    62 	}
    43 	}
    63 
    44 
    64 	return wp_styles()->do_items( $handles );
    45 	return $wp_styles->do_items( $handles );
       
    46 }
    65 }
    47 
    66 
    48 /**
    67 /**
    49  * Add extra CSS styles to a registered stylesheet.
    68  * Add extra CSS styles to a registered stylesheet.
    50  *
    69  *
    52  * Accepts a string $data containing the CSS. If two or more CSS code blocks
    71  * Accepts a string $data containing the CSS. If two or more CSS code blocks
    53  * are added to the same stylesheet $handle, they will be printed in the order
    72  * are added to the same stylesheet $handle, they will be printed in the order
    54  * they were added, i.e. the latter added styles can redeclare the previous.
    73  * they were added, i.e. the latter added styles can redeclare the previous.
    55  *
    74  *
    56  * @see WP_Styles::add_inline_style()
    75  * @see WP_Styles::add_inline_style()
    57  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
       
    58  *
    76  *
    59  * @since 3.3.0
    77  * @since 3.3.0
    60  *
    78  *
    61  * @param string $handle Name of the stylesheet to add the extra styles to. Must be lowercase.
    79  * @param string $handle Name of the stylesheet to add the extra styles to. Must be lowercase.
    62  * @param string $data   String containing the CSS styles to be added.
    80  * @param string $data   String containing the CSS styles to be added.
    63  * @return bool True on success, false on failure.
    81  * @return bool True on success, false on failure.
    64  */
    82  */
    65 function wp_add_inline_style( $handle, $data ) {
    83 function wp_add_inline_style( $handle, $data ) {
    66 	global $wp_styles;
    84 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
    67 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
       
    68 		if ( ! did_action( 'init' ) )
       
    69 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
    70 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
       
    71 		$wp_styles = new WP_Styles();
       
    72 	}
       
    73 
    85 
    74 	if ( false !== stripos( $data, '</style>' ) ) {
    86 	if ( false !== stripos( $data, '</style>' ) ) {
    75 		_doing_it_wrong( __FUNCTION__, 'Do not pass style tags to wp_add_inline_style().', '3.7' );
    87 		_doing_it_wrong( __FUNCTION__, __( 'Do not pass style tags to wp_add_inline_style().' ), '3.7' );
    76 		$data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) );
    88 		$data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) );
    77 	}
    89 	}
    78 
    90 
    79 	return $wp_styles->add_inline_style( $handle, $data );
    91 	return wp_styles()->add_inline_style( $handle, $data );
    80 }
    92 }
    81 
    93 
    82 /**
    94 /**
    83  * Register a CSS stylesheet.
    95  * Register a CSS stylesheet.
    84  *
    96  *
    85  * @see WP_Dependencies::add()
    97  * @see WP_Dependencies::add()
    86  * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
    98  * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
    87  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
       
    88  *
    99  *
    89  * @since 2.6.0
   100  * @since 2.6.0
    90  *
   101  *
    91  * @param string      $handle Name of the stylesheet.
   102  * @param string      $handle Name of the stylesheet.
    92  * @param string|bool $src    Path to the stylesheet from the WordPress root directory. Example: '/css/mystyle.css'.
   103  * @param string|bool $src    Path to the stylesheet from the WordPress root directory. Example: '/css/mystyle.css'.
    96  * @param string      $media  Optional. The media for which this stylesheet has been defined.
   107  * @param string      $media  Optional. The media for which this stylesheet has been defined.
    97  *                            Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print',
   108  *                            Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print',
    98  *                            'screen', 'tty', or 'tv'.
   109  *                            'screen', 'tty', or 'tv'.
    99  */
   110  */
   100 function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
   111 function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
   101 	global $wp_styles;
   112 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   102 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
   113 
   103 		if ( ! did_action( 'init' ) )
   114 	wp_styles()->add( $handle, $src, $deps, $ver, $media );
   104 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   105 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
       
   106 		$wp_styles = new WP_Styles();
       
   107 	}
       
   108 
       
   109 	$wp_styles->add( $handle, $src, $deps, $ver, $media );
       
   110 }
   115 }
   111 
   116 
   112 /**
   117 /**
   113  * Remove a registered stylesheet.
   118  * Remove a registered stylesheet.
   114  *
   119  *
   115  * @see WP_Dependencies::remove()
   120  * @see WP_Dependencies::remove()
   116  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
       
   117  *
   121  *
   118  * @since 2.1.0
   122  * @since 2.1.0
   119  *
   123  *
   120  * @param string $handle Name of the stylesheet to be removed.
   124  * @param string $handle Name of the stylesheet to be removed.
   121  */
   125  */
   122 function wp_deregister_style( $handle ) {
   126 function wp_deregister_style( $handle ) {
   123 	global $wp_styles;
   127 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   124 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
   128 
   125 		if ( ! did_action( 'init' ) )
   129 	wp_styles()->remove( $handle );
   126 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   127 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
       
   128 		$wp_styles = new WP_Styles();
       
   129 	}
       
   130 
       
   131 	$wp_styles->remove( $handle );
       
   132 }
   130 }
   133 
   131 
   134 /**
   132 /**
   135  * Enqueue a CSS stylesheet.
   133  * Enqueue a CSS stylesheet.
   136  *
   134  *
   137  * Registers the style if source provided (does NOT overwrite) and enqueues.
   135  * Registers the style if source provided (does NOT overwrite) and enqueues.
   138  *
   136  *
   139  * @see WP_Dependencies::add(), WP_Dependencies::enqueue()
   137  * @see WP_Dependencies::add(), WP_Dependencies::enqueue()
   140  * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
   138  * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
   141  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
       
   142  *
   139  *
   143  * @since 2.6.0
   140  * @since 2.6.0
   144  *
   141  *
   145  * @param string      $handle Name of the stylesheet.
   142  * @param string      $handle Name of the stylesheet.
   146  * @param string|bool $src    Path to the stylesheet from the root directory of WordPress. Example: '/css/mystyle.css'.
   143  * @param string|bool $src    Path to the stylesheet from the root directory of WordPress. Example: '/css/mystyle.css'.
   152  *                            Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print',
   149  *                            Default 'all'. Accepts 'all', 'aural', 'braille', 'handheld', 'projection', 'print',
   153  *                            'screen', 'tty', or 'tv'.
   150  *                            'screen', 'tty', or 'tv'.
   154  */
   151  */
   155 function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
   152 function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
   156 	global $wp_styles;
   153 	global $wp_styles;
   157 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
   154 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   158 		if ( ! did_action( 'init' ) )
   155 
   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.' ),
   156 	$wp_styles = wp_styles();
   160 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
       
   161 		$wp_styles = new WP_Styles();
       
   162 	}
       
   163 
   157 
   164 	if ( $src ) {
   158 	if ( $src ) {
   165 		$_handle = explode('?', $handle);
   159 		$_handle = explode('?', $handle);
   166 		$wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
   160 		$wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
   167 	}
   161 	}
   170 
   164 
   171 /**
   165 /**
   172  * Remove a previously enqueued CSS stylesheet.
   166  * Remove a previously enqueued CSS stylesheet.
   173  *
   167  *
   174  * @see WP_Dependencies::dequeue()
   168  * @see WP_Dependencies::dequeue()
   175  * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
       
   176  *
   169  *
   177  * @since 3.1.0
   170  * @since 3.1.0
   178  *
   171  *
   179  * @param string $handle Name of the stylesheet to be removed.
   172  * @param string $handle Name of the stylesheet to be removed.
   180  */
   173  */
   181 function wp_dequeue_style( $handle ) {
   174 function wp_dequeue_style( $handle ) {
   182 	global $wp_styles;
   175 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   183 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
   176 
   184 		if ( ! did_action( 'init' ) )
   177 	wp_styles()->dequeue( $handle );
   185 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   186 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
       
   187 		$wp_styles = new WP_Styles();
       
   188 	}
       
   189 
       
   190 	$wp_styles->dequeue( $handle );
       
   191 }
   178 }
   192 
   179 
   193 /**
   180 /**
   194  * Check whether a CSS stylesheet has been added to the queue.
   181  * Check whether a CSS stylesheet has been added to the queue.
   195  *
   182  *
   201  * @param string $list   Optional. Status of the stylesheet to check. Default 'enqueued'.
   188  * @param string $list   Optional. Status of the stylesheet to check. Default 'enqueued'.
   202  *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
   189  *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
   203  * @return bool Whether style is queued.
   190  * @return bool Whether style is queued.
   204  */
   191  */
   205 function wp_style_is( $handle, $list = 'enqueued' ) {
   192 function wp_style_is( $handle, $list = 'enqueued' ) {
   206 	global $wp_styles;
   193 	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
   207 	if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
   194 
   208 		if ( ! did_action( 'init' ) )
   195 	return (bool) wp_styles()->query( $handle, $list );
   209 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   210 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
       
   211 		$wp_styles = new WP_Styles();
       
   212 	}
       
   213 
       
   214 	return (bool) $wp_styles->query( $handle, $list );
       
   215 }
   196 }
   216 
   197 
   217 /**
   198 /**
   218  * Add metadata to a CSS stylesheet.
   199  * Add metadata to a CSS stylesheet.
   219  *
   200  *
   231  * @since 3.6.0
   212  * @since 3.6.0
   232  *
   213  *
   233  * @param string $handle Name of the stylesheet.
   214  * @param string $handle Name of the stylesheet.
   234  * @param string $key    Name of data point for which we're storing a value.
   215  * @param string $key    Name of data point for which we're storing a value.
   235  *                       Accepts 'conditional', 'rtl' and 'suffix', 'alt' and 'title'.
   216  *                       Accepts 'conditional', 'rtl' and 'suffix', 'alt' and 'title'.
   236  * @param mixed  $data   String containing the CSS data to be added.
   217  * @param mixed  $value  String containing the CSS data to be added.
   237  * @return bool True on success, false on failure.
   218  * @return bool True on success, false on failure.
   238  */
   219  */
   239 function wp_style_add_data( $handle, $key, $value ) {
   220 function wp_style_add_data( $handle, $key, $value ) {
   240 	global $wp_styles;
   221 	return wp_styles()->add_data( $handle, $key, $value );
   241 	return $wp_styles->add_data( $handle, $key, $value );
   222 }
   242 }