Introduce new registration methods for custom headers and custom backgrounds. Backwards compatible, but old methods will be deprecated. see #20249. see #17242.

Custom header: Use add_theme_support('custom-header', $args) instead of add_custom_image_header(). Deprecates all use of constants.
 * HEADER_TEXTCOLOR is now (string) 'default-text-color'.
 * NO_HEADER_TEXT is nowi ! (bool) 'header-text'.
 * HEADER_IMAGE_WIDTH (and _HEIGHT) are now (int) 'width' and 'height'.
 * HEADER_IMAGE is now (string) 'default-image'.
 * The 3.4 arguments 'suggested-width' and 'suggested-height' are now just 'width' and 'height' (they are "suggested" when flex-width and flex-height are set).
 * Callback arguments for add_custom_image_header() can now be passed to add_theme_support().

Custom background: Use add_theme_support('custom-background, $args) instead of add_custom_background(). Deprecates all use of constants.
 * BACKGROUND_COLOR is now (string) 'default-color'.
 * BACKGROUND_IMAGE is now (string) 'default-image'.
 * Callback arguments for add_custom_background() can now be passed to add_theme_support().

Inheritance: add_theme_support() arguments for custom headers and custom backgrounds is a first-one-wins situation. This is not an unusual paradigm for theming as a child theme (which is included first) overrides a parent theme.
 * Once an argument is explicitly set, it cannot be overridden. You must hook in earlier and set it first.
 * Any argument that is not explicitly set before WP is loaded will inherit the default value for that argument.
 * It is therefore possible for a child theme to pass minimal arguments as long as the parent theme specifies others that may be necessary.
 * Allows for a child theme to alter callbacks for <head> and preview (previously, calling add_custom_image_header more than once broke things).
 * The just-in-time bits ensure that arguments fall back to default values, that the values of all constants are considered (such as one defined after an old add_custom_image_header call), and that all constants are defined (so as to be backwards compatible).

get_theme_support(): Introduce new second argument, which headers and backgrounds leverage to return an argument. current_theme_supports() already supported checking the truthiness of the argument.
 * For example, get_theme_support( 'custom-header', 'width' ) will return the width specified during registration.
 * If you had wanted the default image, use get_theme_support( 'custom-header', 'default-image' ) instead of HEADER_IMAGE. 

Deprecate remove_custom_image_header(), remove_custom_background(). Use remove_theme_support('custom-header'), 'custom-background'.

Deprecate short-lived custom-header-uploads internal support; this is now (bool) 'uploads' for add_theme_support().

New 3.4 functions renamed or removed: Rename get_current_header_data() to get_custom_header(). Remove get_header_image_width() and _height() in favor of get_custom_header()->width and height.



git-svn-id: https://develop.svn.wordpress.org/trunk@20212 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2012-03-19 17:12:44 +00:00
parent 866e4df183
commit 070a9e5c59
4 changed files with 308 additions and 200 deletions

View File

@ -53,6 +53,8 @@ class Custom_Background {
function __construct($admin_header_callback = '', $admin_image_div_callback = '') {
$this->admin_header_callback = $admin_header_callback;
$this->admin_image_div_callback = $admin_image_div_callback;
add_action( 'admin_menu', array( $this, 'init' ) );
}
/**
@ -226,7 +228,7 @@ if ( get_background_image() ) {
</tr>
<?php endif; ?>
<?php if ( defined( 'BACKGROUND_IMAGE' ) ) : // Show only if a default background image exists ?>
<?php if ( get_theme_support( 'custom-background', 'default-image' ) ) : ?>
<tr valign="top">
<th scope="row"><?php _e('Restore Original Image'); ?></th>
<td>

View File

@ -71,6 +71,8 @@ class Custom_Image_Header {
function __construct($admin_header_callback, $admin_image_div_callback = '') {
$this->admin_header_callback = $admin_header_callback;
$this->admin_image_div_callback = $admin_image_div_callback;
add_action( 'admin_menu', array( $this, 'init' ) );
}
/**
@ -140,7 +142,7 @@ class Custom_Image_Header {
function js_includes() {
$step = $this->step();
if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
wp_enqueue_script('farbtastic');
elseif ( 2 == $step )
wp_enqueue_script('imgareaselect');
@ -154,24 +156,12 @@ class Custom_Image_Header {
function css_includes() {
$step = $this->step();
if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
wp_enqueue_style('farbtastic');
elseif ( 2 == $step )
wp_enqueue_style('imgareaselect');
}
/**
* Check if header text is allowed
*
* @since 3.0.0
*/
function header_text() {
if ( defined( 'NO_HEADER_TEXT' ) && NO_HEADER_TEXT )
return false;
return true;
}
/**
* Execute custom header modification.
*
@ -189,7 +179,7 @@ class Custom_Image_Header {
if ( isset( $_POST['resetheader'] ) ) {
check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
$this->process_default_headers();
$default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : '';
$default = get_theme_support( 'custom-header', 'default-image' );
$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
foreach ( $this->default_headers as $header => $details ) {
if ( $details['url'] == $default ) {
@ -199,9 +189,9 @@ class Custom_Image_Header {
}
set_theme_mod( 'header_image', $default );
if ( empty( $default_data['width'] ) )
$default_data['width'] = HEADER_IMAGE_WIDTH;
$default_data['width'] = get_theme_support( 'custom-header', 'width' );
if ( empty( $default_data['height'] ) )
$default_data['height'] = HEADER_IMAGE_HEIGHT;
$default_data['height'] = get_theme_support( 'custom-header', 'height' );
set_theme_mod( 'header_image_data', (object) $default_data );
return;
}
@ -245,9 +235,9 @@ class Custom_Image_Header {
} elseif ( isset( $this->default_headers[$_POST['default-header']] ) ) {
set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) );
if ( empty( $this->default_headers[$_POST['default-header']]['width'] ) )
$this->default_headers[$_POST['default-header']]['width'] = HEADER_IMAGE_WIDTH;
$this->default_headers[$_POST['default-header']]['width'] = get_theme_support( 'custom-header', 'width' );
if ( empty( $this->default_headers[$_POST['default-header']]['height'] ) )
$this->default_headers[$_POST['default-header']]['height'] = HEADER_IMAGE_HEIGHT;
$this->default_headers[$_POST['default-header']]['height'] = get_theme_support( 'custom-header', 'height' );
set_theme_mod( 'header_image_data', (object) $this->default_headers[$_POST['default-header']] );
}
}
@ -325,7 +315,7 @@ class Custom_Image_Header {
*/
function js() {
$step = $this->step();
if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
$this->js_1();
elseif ( 2 == $step )
$this->js_2();
@ -341,7 +331,7 @@ class Custom_Image_Header {
/* <![CDATA[ */
var text_objects = ['#name', '#desc', '#text-color-row'];
var farbtastic;
var default_color = '#<?php echo HEADER_TEXTCOLOR; ?>';
var default_color = '#<?php echo get_theme_support( 'custom-header', 'default-text-color' ); ?>';
var old_color = null;
function pickColor(color) {
@ -411,11 +401,11 @@ class Custom_Image_Header {
});
farbtastic = jQuery.farbtastic('#color-picker', function(color) { pickColor(color); });
<?php if ( $color = get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) ) { ?>
<?php if ( $color = get_header_textcolor() ) { ?>
pickColor('#<?php echo $color; ?>');
<?php } ?>
<?php if ( 'blank' == get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) || '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || ! $this->header_text() ) { ?>
<?php if ( 'blank' == $color || '' == $color || ! current_theme_supports( 'custom-header', 'header-text' ) ) { ?>
toggle_text();
<?php } ?>
});
@ -440,17 +430,8 @@ class Custom_Image_Header {
}
jQuery(document).ready(function() {
<?php
$xinit = HEADER_IMAGE_WIDTH;
$yinit = HEADER_IMAGE_HEIGHT;
$header_support = get_theme_support( 'custom-header' );
if ( !empty( $header_support[ 0 ][ 'suggested-width' ] ) )
$xinit = $header_support[ 0 ][ 'suggested-width' ];
if ( !empty( $header_support[ 0 ][ 'suggested-height' ] ) )
$yinit = $header_support[ 0 ][ 'suggested-height' ];
?>
var xinit = <?php echo absint( $xinit ); ?>;
var yinit = <?php echo absint( $yinit ); ?>;
var xinit = <?php echo absint( get_theme_support( 'custom-header', 'width' ) ); ?>;
var yinit = <?php echo absint( get_theme_support( 'custom-header', 'height' ) ); ?>;
var ratio = xinit / yinit;
var ximg = jQuery('img#upload').width();
var yimg = jQuery('img#upload').height();
@ -481,12 +462,12 @@ class Custom_Image_Header {
}
if ( ! current_theme_supports( 'custom-header', 'flex-height' ) ) {
?>
maxHeight: <?php echo HEADER_IMAGE_HEIGHT; ?>,
maxHeight: <?php echo get_theme_support( 'custom-header', 'height' ); ?>,
<?php
}
if ( ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
?>
maxWidth: <?php echo HEADER_IMAGE_WIDTH; ?>,
maxWidth: <?php echo get_theme_support( 'custom-header', 'width' ); ?>,
<?php
}
?>
@ -531,17 +512,18 @@ class Custom_Image_Header {
<tr valign="top">
<th scope="row"><?php _e( 'Preview' ); ?></th>
<td >
<td>
<?php if ( $this->admin_image_div_callback ) {
call_user_func( $this->admin_image_div_callback );
} else {
?>
<div id="headimg" style="background-image:url(<?php esc_url ( header_image() ) ?>);max-width:<?php echo get_header_image_width(); ?>px;height:<?php echo get_header_image_height(); ?>px;">
<div id="headimg" style="background-image:url(<?php esc_url ( header_image() ) ?>);max-width:<?php echo get_custom_header()->width; ?>px;height:<?php echo get_custom_header()->height; ?>px;">
<?php
if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || ! $this->header_text() )
$color = get_header_textcolor();
if ( 'blank' == $color || '' == $color || ! current_theme_supports( 'custom-header', 'header-text' ) )
$style = ' style="display:none;"';
else
$style = ' style="color:#' . get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) . ';"';
$style = ' style="color:#' . $color . ';"';
?>
<h1><a id="name"<?php echo $style; ?> onclick="return false;" href="<?php bloginfo('url'); ?>"><?php bloginfo( 'name' ); ?></a></h1>
<div id="desc"<?php echo $style; ?>><?php bloginfo( 'description' ); ?></div>
@ -549,27 +531,26 @@ class Custom_Image_Header {
<?php } ?>
</td>
</tr>
<?php if ( current_theme_supports( 'custom-header-uploads' ) ) : ?>
<?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
<tr valign="top">
<th scope="row"><?php _e( 'Upload Image' ); ?></th>
<td>
<p><?php _e( 'You can upload a custom header image to be shown at the top of your site instead of the default one. On the next screen you will be able to crop the image.' ); ?><br />
<?php
if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) {
printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ) . '<br />', HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT );
printf( __( 'Images of exactly <strong>%1$d &times; %2$d pixels</strong> will be used as-is.' ) . '<br />', get_theme_support( 'custom-header', 'width' ), get_theme_support( 'custom-header', 'height' ) );
} elseif ( current_theme_supports( 'custom-header', 'flex-height' ) ) {
if ( ! current_theme_supports( 'custom-header', 'flex-width' ) )
printf( __( 'Images should be at least <strong>%1$d pixels</strong> wide.' ) . '<br />', HEADER_IMAGE_WIDTH );
printf( __( 'Images should be at least <strong>%1$d pixels</strong> wide.' ) . ' ', get_theme_support( 'custom-header', 'width' ) );
} elseif ( current_theme_supports( 'custom-header', 'flex-width' ) ) {
if ( ! current_theme_supports( 'custom-header', 'flex-height' ) )
printf( __( 'Images should be at least <strong>%1$d pixels</strong> tall.' ) . '<br />', HEADER_IMAGE_HEIGHT );
printf( __( 'Images should be at least <strong>%1$d pixels</strong> tall.' ) . ' ', get_theme_support( 'custom-header', 'height' ) );
}
if ( current_theme_supports( 'custom-header', 'flex-height' ) || current_theme_supports( 'custom-header', 'flex-width' ) ) {
$header_support = get_theme_support( 'custom-header' );
if ( !empty( $header_support[ 0 ][ 'suggested-width' ] ) )
printf( __( 'Suggested width is <strong>%1$d pixels</strong>.' ) . '<br />', absint( $header_support[ 0 ][ 'suggested-width' ] ) );
if ( !empty( $header_support[ 0 ][ 'suggested-height' ] ) )
printf( __( 'Suggested height is <strong>%1$d pixels</strong>.' ) . '<br />', absint( $header_support[ 0 ][ 'suggested-height' ] ) );
if ( current_theme_supports( 'custom-header', 'width' ) )
printf( __( 'Suggested width is <strong>%1$d pixels</strong>.' ) . ' ', get_theme_support( 'custom-header', 'width' ) );
if ( current_theme_supports( 'custom-header', 'height' ) )
printf( __( 'Suggested height is <strong>%1$d pixels</strong>.' ) . ' ', get_theme_support( 'custom-header', 'height' ) );
}
?></p>
<form enctype="multipart/form-data" id="upload-form" method="post" action="<?php echo esc_attr( add_query_arg( 'step', 2 ) ) ?>">
@ -605,7 +586,7 @@ class Custom_Image_Header {
<tr valign="top">
<th scope="row"><?php _e( 'Default Images' ); ?></th>
<td>
<?php if ( current_theme_supports( 'custom-header-uploads' ) ) : ?>
<?php if ( current_theme_supports( 'custom-header', 'uploads' ) ) : ?>
<p><?php _e( 'If you don&lsquo;t want to upload your own image, you can use one of these cool headers, or show a random one.' ) ?></p>
<?php else: ?>
<p><?php _e( 'You can use one of these cool headers or show a random one on each page.' ) ?></p>
@ -626,7 +607,7 @@ class Custom_Image_Header {
</tr>
<?php endif;
if ( defined( 'HEADER_IMAGE' ) && '' != HEADER_IMAGE ) : ?>
if ( current_theme_supports( 'custom-header', 'default-image' ) ) : ?>
<tr valign="top">
<th scope="row"><?php _e( 'Reset Image' ); ?></th>
<td>
@ -637,16 +618,22 @@ class Custom_Image_Header {
<?php endif; ?>
</tbody>
</table>
<?php if ( $this->header_text() ) : ?>
<?php if ( current_theme_supports( 'custom-header', 'header-text' ) ) : ?>
<table class="form-table">
<tbody>
<tr valign="top" class="hide-if-no-js">
<th scope="row"><?php _e( 'Display Text' ); ?></th>
<td>
<p>
<?php $hidetext = get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ); ?>
<label><input type="radio" value="1" name="hidetext" id="hidetext"<?php checked( ( 'blank' == $hidetext || empty( $hidetext ) ) ? true : false ); ?> /> <?php _e( 'No' ); ?></label>
<label><input type="radio" value="0" name="hidetext" id="showtext"<?php checked( ( 'blank' == $hidetext || empty( $hidetext ) ) ? false : true ); ?> /> <?php _e( 'Yes' ); ?></label>
<?php
$show_text = get_header_textcolor();
if ( 'blank' == $show_text )
$show_text = false;
else
$show_text = (bool) $show_text;
?>
<label><input type="radio" value="1" name="hidetext" id="hidetext"<?php checked( ! $show_text ); ?> /> <?php _e( 'No' ); ?></label>
<label><input type="radio" value="0" name="hidetext" id="showtext"<?php checked( $show_text ); ?> /> <?php _e( 'Yes' ); ?></label>
</p>
</td>
</tr>
@ -655,7 +642,7 @@ class Custom_Image_Header {
<th scope="row"><?php _e( 'Text Color' ); ?></th>
<td>
<p>
<input type="text" name="text-color" id="text-color" value="#<?php echo esc_attr( get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) ); ?>" />
<input type="text" name="text-color" id="text-color" value="#<?php echo esc_attr( get_header_textcolor() ); ?>" />
<span class="description hide-if-js"><?php _e( 'If you want to hide header text, add <strong>#blank</strong> as text color.' );?></span>
<input type="button" class="button hide-if-no-js" value="<?php esc_attr_e( 'Select a Color' ); ?>" id="pickcolor" />
</p>
@ -663,7 +650,7 @@ class Custom_Image_Header {
</td>
</tr>
<?php if ( defined('HEADER_TEXTCOLOR') && get_theme_mod('header_textcolor') ) { ?>
<?php if ( current_theme_supports( 'custom-header', 'default-text-color' ) && get_theme_mod( 'header_textcolor' ) ) { ?>
<tr valign="top">
<th scope="row"><?php _e('Reset Text Color'); ?></th>
<td>
@ -694,7 +681,7 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
*/
function step_2() {
check_admin_referer('custom-header-upload', '_wpnonce-custom-header-upload');
if ( ! current_theme_supports( 'custom-header-uploads' ) )
if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
wp_die( __( 'Cheatin&#8217; uh?' ) );
$overrides = array('test_form' => false);
@ -722,19 +709,19 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
list($width, $height, $type, $attr) = getimagesize( $file );
$header_support = get_theme_support( 'custom-header' );
$max_width = 0;
// For flex, limit size of image displayed to 1500px unless theme says otherwise
if ( current_theme_supports( 'custom-header', 'flex-width' ) )
$max_width = 1500;
if ( !empty( $header_support[ 0 ][ 'max-width' ] ) )
$max_width = max( $max_width, absint( $header_support[ 0 ][ 'max-width' ] ) );
if ( current_theme_supports( 'custom-header', 'max-width' ) )
$max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
$max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
if ( defined( 'HEADER_IMAGE_WIDTH' ) )
$max_width = max( $max_width, HEADER_IMAGE_WIDTH );
// If flexible height isn't supported and the image is the exact right size
if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) && $width == HEADER_IMAGE_WIDTH && $height == HEADER_IMAGE_HEIGHT ) {
if ( ! current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' )
&& $width == get_theme_support( 'custom-header', 'width' ) && $height == get_theme_support( 'custom-header', 'height' ) )
{
// Add the meta-data
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
update_post_meta( $id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
@ -791,8 +778,9 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
* @since 2.1.0
*/
function step_3() {
check_admin_referer('custom-header-crop-image');
if ( ! current_theme_supports( 'custom-header-uploads' ) )
check_admin_referer( 'custom-header-crop-image' );
if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
wp_die( __( 'Cheatin&#8217; uh?' ) );
if ( $_POST['oitar'] > 1 ) {
@ -805,31 +793,29 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
$attachment_id = absint( $_POST['attachment_id'] );
$original = get_attached_file($attachment_id);
$header_support = get_theme_support( 'custom-header' );
$max_width = 0;
// For flex, limit size of image displayed to 1500px unless theme says otherwise
if ( current_theme_supports( 'custom-header', 'flex-width' ) )
$max_width = 1500;
if ( !empty( $header_support[ 0 ][ 'max-width' ] ) )
$max_width = max( $max_width, absint( $header_support[ 0 ][ 'max-width' ] ) );
if ( defined( 'HEADER_IMAGE_WIDTH' ) )
$max_width = max( $max_width, HEADER_IMAGE_WIDTH );
if ( current_theme_supports( 'custom-header', 'max-width' ) )
$max_width = max( $max_width, get_theme_support( 'custom-header', 'max-width' ) );
$max_width = max( $max_width, get_theme_support( 'custom-header', 'width' ) );
if ( ( current_theme_supports( 'custom-header', 'flex-height' ) && ! current_theme_supports( 'custom-header', 'flex-width' ) ) || $_POST['width'] > $max_width )
$dst_height = absint( $_POST['height'] * ( $max_width / $_POST['width'] ) );
elseif ( current_theme_supports( 'custom-header', 'flex-height' ) && current_theme_supports( 'custom-header', 'flex-width' ) )
$dst_height = absint( $_POST['height'] );
else
$dst_height = HEADER_IMAGE_HEIGHT;
$dst_height = get_theme_support( 'custom-header', 'height' );
if ( ( current_theme_supports( 'custom-header', 'flex-width' ) && ! current_theme_supports( 'custom-header', 'flex-height' ) ) || $_POST['width'] > $max_width )
$dst_width = absint( $_POST['width'] * ( $max_width / $_POST['width'] ) );
elseif ( current_theme_supports( 'custom-header', 'flex-width' ) && current_theme_supports( 'custom-header', 'flex-height' ) )
$dst_width = absint( $_POST['width'] );
else
$dst_width = HEADER_IMAGE_WIDTH;
$dst_width = get_theme_support( 'custom-header', 'width' );
$cropped = wp_crop_image( $attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $dst_width, $dst_height );
if ( is_wp_error( $cropped ) )

View File

@ -479,7 +479,7 @@ final class WP_Customize {
'section' => 'header',
'sanitize_callback' => 'sanitize_hexcolor',
'control' => 'color',
'default' => defined( 'HEADER_TEXTCOLOR' ) ? HEADER_TEXTCOLOR : ''
'default' => get_theme_support( 'custom-header', 'default-text-color' ),
) );
/*
@ -505,7 +505,7 @@ final class WP_Customize {
'control' => 'checkbox',
// @todo
// not the default, it's the value.
// value is saved in get_theme_support( 'custom-header' )[0][ 'random-default' ]
// value is saved in get_theme_support( 'custom-header', 'random-default' )
'default' => 'random-default-image'
) );
@ -522,7 +522,7 @@ final class WP_Customize {
'label' => 'Background Color',
'section' => 'background',
'control' => 'color',
'default' => defined( 'BACKGROUND_COLOR' ) ? BACKGROUND_COLOR : '',
'default' => get_theme_support( 'custom-background', 'default-color' ),
'sanitize_callback' => 'sanitize_hexcolor',
) );

View File

@ -901,14 +901,11 @@ function remove_theme_mods() {
* Retrieve text color for custom header.
*
* @since 2.1.0
* @uses HEADER_TEXTCOLOR
*
* @return string
*/
function get_header_textcolor() {
$default = defined('HEADER_TEXTCOLOR') ? HEADER_TEXTCOLOR : '';
return get_theme_mod('header_textcolor', $default);
return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
}
/**
@ -924,13 +921,11 @@ function header_textcolor() {
* Retrieve header image for custom header.
*
* @since 2.1.0
* @uses HEADER_IMAGE
*
* @return string
*/
function get_header_image() {
$default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : '';
$url = get_theme_mod( 'header_image', $default );
$url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
if ( 'remove-header' == $url )
return false;
@ -970,8 +965,7 @@ function _get_random_header_data() {
if ( 'random-default-image' == $header_image_mod ) {
$headers = $_wp_default_headers;
} else {
$is_random = get_theme_support( 'custom-header' );
if ( isset( $is_random[ 0 ] ) && !empty( $is_random[ 0 ][ 'random-default' ] ) )
if ( current_theme_supports( 'custom-header', 'random-default' ) )
$headers = $_wp_default_headers;
}
}
@ -1010,14 +1004,12 @@ function get_random_header_image() {
* is chosen, and theme turns on random headers with add_theme_support().
*
* @since 3.2.0
* @uses HEADER_IMAGE
*
* @param string $type The random pool to use. any|default|uploaded
* @return boolean
*/
function is_random_header_image( $type = 'any' ) {
$default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : '';
$header_image_mod = get_theme_mod( 'header_image', $default );
$header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
if ( 'any' == $type ) {
if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
@ -1079,98 +1071,51 @@ function get_uploaded_header_images() {
*
* @return object
*/
function get_current_header_data() {
function get_custom_header() {
$data = is_random_header_image()? _get_random_header_data() : get_theme_mod( 'header_image_data' );
$default = array(
'url' => '',
'thumbnail_url' => '',
'width' => '',
'height' => '',
'width' => get_theme_suppport( 'custom-header', 'width' ),
'height' => get_theme_suppport( 'custom-header', 'height' ),
);
return (object) wp_parse_args( $data, $default );
}
/**
* Get the header image width.
*
* @since 3.4.0
*
* @return int
*/
function get_header_image_width() {
return empty( get_current_header_data()->width )? HEADER_IMAGE_WIDTH : get_current_header_data()->width;
}
/**
* Get the header image height.
*
* @since 3.4.0
*
* @return int
*/
function get_header_image_height() {
return empty( get_current_header_data()->height )? HEADER_IMAGE_HEIGHT : get_current_header_data()->height;
}
/**
* Add callbacks for image header display.
*
* The parameter $header_callback callback will be required to display the
* content for the 'wp_head' action. The parameter $admin_header_callback
* callback will be added to Custom_Image_Header class and that will be added
* to the 'admin_menu' action.
*
* @since 2.1.0
* @uses Custom_Image_Header Sets up for $admin_header_callback for administration panel display.
* @deprecated 3.4.0
* @deprecated Use add_theme_support('custom-header', $args)
* @see add_theme_support()
*
* @param callback $header_callback Call on 'wp_head' action.
* @param callback $admin_header_callback Call on custom header administration screen.
* @param callback $admin_image_div_callback Output a custom header image div on the custom header administration screen. Optional.
*/
function add_custom_image_header( $header_callback, $admin_header_callback, $admin_image_div_callback = '' ) {
if ( ! empty( $header_callback ) )
add_action('wp_head', $header_callback);
$support = array( 'callback' => $header_callback );
$theme_support = get_theme_support( 'custom-header' );
if ( ! empty( $theme_support ) && is_array( $theme_support[ 0 ] ) )
$support = array_merge( $theme_support[ 0 ], $support );
add_theme_support( 'custom-header', $support );
add_theme_support( 'custom-header-uploads' );
if ( ! is_admin() )
return;
global $custom_image_header;
require_once( ABSPATH . 'wp-admin/custom-header.php' );
$custom_image_header = new Custom_Image_Header( $admin_header_callback, $admin_image_div_callback );
add_action( 'admin_menu', array( &$custom_image_header, 'init' ) );
# _deprecated_function( __FUNCTION__, '3.4', 'add_theme_support(\'custom-header\', $args)' );
return add_theme_support( 'custom-header', array(
'callback' => $header_callback,
'admin-header-callback' => $admin_header_callback,
'admin-image-div-callback' => $admin_image_div_callback,
) );
}
/**
* Remove image header support.
*
* @since 3.1.0
* @see add_custom_image_header()
* @deprecated 3.4.0
* @deprecated Use remove_theme_support('custom-header')
* @see remove_theme_support()
*
* @return bool Whether support was removed.
*/
function remove_custom_image_header() {
if ( ! current_theme_supports( 'custom-header' ) )
return false;
$callback = get_theme_support( 'custom-header' );
remove_action( 'wp_head', $callback[0]['callback'] );
_remove_theme_support( 'custom-header' );
remove_theme_support( 'custom-header-uploads' );
if ( is_admin() ) {
remove_action( 'admin_menu', array( &$GLOBALS['custom_image_header'], 'init' ) );
unset( $GLOBALS['custom_image_header'] );
}
return true;
# _deprecated_function( __FUNCTION__, '3.4', 'remove_theme_support(\'custom-header\')' );
return remove_theme_support( 'custom-header' );
}
/**
@ -1218,9 +1163,7 @@ function unregister_default_headers( $header ) {
* @return string
*/
function get_background_image() {
$default = defined('BACKGROUND_IMAGE') ? BACKGROUND_IMAGE : '';
return get_theme_mod('background_image', $default);
return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
}
/**
@ -1236,14 +1179,11 @@ function background_image() {
* Retrieve value for custom background color.
*
* @since 3.0.0
* @uses BACKGROUND_COLOR
*
* @return string
*/
function get_background_color() {
$default = defined('BACKGROUND_COLOR') ? BACKGROUND_COLOR : '';
return get_theme_mod('background_color', $default);
return get_theme_mod('background_color', get_theme_support( 'custom-background', 'default-color' ) );
}
/**
@ -1271,21 +1211,12 @@ function background_color() {
* @param callback $admin_image_div_callback Output a custom background image div on the custom background administration screen. Optional.
*/
function add_custom_background( $header_callback = '', $admin_header_callback = '', $admin_image_div_callback = '' ) {
if ( isset( $GLOBALS['custom_background'] ) )
return;
if ( empty( $header_callback ) )
$header_callback = '_custom_background_cb';
add_action( 'wp_head', $header_callback );
add_theme_support( 'custom-background', array( 'callback' => $header_callback ) );
if ( ! is_admin() )
return;
require_once( ABSPATH . 'wp-admin/custom-background.php' );
$GLOBALS['custom_background'] = new Custom_Background( $admin_header_callback, $admin_image_div_callback );
add_action( 'admin_menu', array( &$GLOBALS['custom_background'], 'init' ) );
# _deprecated_function( __FUNCTION__, '3.4', 'add_theme_support(\'custom-background\', $args)' );
return add_theme_support( 'custom-background', array(
'callback' => $header_callback,
'admin-header-callback' => $admin_header_callback,
'admin-image-div-callback' => $admin_image_div_callback,
) );
}
/**
@ -1297,26 +1228,14 @@ function add_custom_background( $header_callback = '', $admin_header_callback =
* @return bool Whether support was removed.
*/
function remove_custom_background() {
if ( ! current_theme_supports( 'custom-background' ) )
return false;
$callback = get_theme_support( 'custom-background' );
remove_action( 'wp_head', $callback[0]['callback'] );
_remove_theme_support( 'custom-background' );
if ( is_admin() ) {
remove_action( 'admin_menu', array( &$GLOBALS['custom_background'], 'init' ) );
unset( $GLOBALS['custom_background'] );
}
return true;
# _deprecated_function( __FUNCTION__, '3.4', 'remove_theme_support(\'custom-background\')' );
return remove_theme_support( 'custom-background' );
}
/**
* Default custom background callback.
*
* @since 3.0.0
* @see add_custom_background()
* @access protected
*/
function _custom_background_cb() {
@ -1422,14 +1341,171 @@ function add_theme_support( $feature ) {
global $_wp_theme_features;
if ( func_num_args() == 1 )
$_wp_theme_features[$feature] = true;
$args = true;
else
$_wp_theme_features[$feature] = array_slice( func_get_args(), 1 );
$args = array_slice( func_get_args(), 1 );
if ( $feature == 'post-formats' && is_array( $_wp_theme_features[$feature][0] ) )
$_wp_theme_features[$feature][0] = array_intersect( $_wp_theme_features[$feature][0], array_keys( get_post_format_slugs() ) );
switch ( $feature ) {
case 'post-formats' :
if ( is_array( $args[0] ) )
$args[0] = array_intersect( $args[0], array_keys( get_post_format_slugs() ) );
break;
case 'custom-header-uploads' :
return add_theme_support( 'custom-header', array( 'uploads' => true ) );
break;
case 'custom-header' :
$defaults = array(
'default-image' => '',
'random-default' => false,
'width' => 0,
'height' => 0,
'flex-height' => false,
'flex-width' => false,
'default-text-color' => '',
'header-text' => true,
'uploads' => true,
'callback' => '',
'admin-header-callback' => '',
'admin-image-div-callback' => '',
);
$jit = isset( $args[0]['__jit'] );
unset( $args[0]['__jit'] );
// Merge in data from previous add_theme_support() calls.
// The first value registered wins. (A child theme is set up first.)
if ( isset( $_wp_theme_features['custom-header'] ) )
$args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
// Load in the defaults at the end, as we need to insure first one wins.
// This will cause all constants to be defined, as each arg will then be set to the default.
if ( $jit )
$args[0] = wp_parse_args( $args[0], $defaults );
// If a constant was defined, use that value. Otherwise, define the constant to ensure
// the constant is always accurate (and is not defined later, overriding our value).
// As stated above, the first value wins.
// Once we get to wp_loaded (just-in-time), define any constants we haven't already.
// Constants are lame. Don't reference them. This is just for backwards compatibility.
if ( defined( 'NO_HEADER_TEXT' ) )
$args[0]['header-text'] = ! NO_HEADER_TEXT;
elseif ( isset( $args[0]['header-text'] ) )
define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
if ( defined( 'HEADER_IMAGE_WIDTH' ) )
$args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
elseif ( isset( $args[0]['width'] ) )
define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
if ( defined( 'HEADER_IMAGE_HEIGHT' ) )
$args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
elseif ( ! isset( $args[0]['height'] ) )
define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
if ( defined( 'HEADER_TEXTCOLOR' ) )
$args[0]['default-text-color'] = HEADER_TEXTCOLOR;
elseif ( isset( $args[0]['default-text-color'] ) )
define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
if ( defined( 'HEADER_IMAGE' ) )
$args[0]['default-image'] = HEADER_IMAGE;
if ( $jit && ! empty( $args[0]['default-image'] ) )
$args[0]['random-default'] = false;
if ( ! defined( 'HEADER_IMAGE' ) && ( isset( $args[0]['default-image'] ) || isset( $args[0]['random-default'] ) ) )
define( 'HEADER_IMAGE', $args[0]['default-image'] );
// If headers are supported, and we still don't have a defined width or height,
// we have implicit flex sizes.
if ( $jit ) {
if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) )
$args[0]['flex-width'] = true;
if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) )
$args[0]['flex-height'] = true;
}
break;
case 'custom-background' :
$defaults = array(
'default-image' => '',
'default-color' => '',
'callback' => '',
'admin-header-callback' => '',
'admin-image-div-callback' => '',
);
$jit = isset( $args[0]['__jit'] );
unset( $args[0]['__jit'] );
// Merge in data from previous add_theme_support() calls. The first value registered wins.
if ( isset( $_wp_theme_features['custom-background'] ) )
$args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
if ( $jit )
$args[0] = wp_parse_args( $args[0], $defaults );
if ( defined( 'BACKGROUND_COLOR' ) )
$args[0]['default-color'] = BACKGROUND_COLOR;
elseif ( isset( $args[0]['default-color'] ) || $jit )
define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
if ( defined( 'BACKGROUND_IMAGE' ) )
$args[0]['default-image'] = BACKGROUND_IMAGE;
elseif ( isset( $args[0]['default-image'] ) || $jit )
define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
if ( empty( $args[0]['callback'] ) )
$args[0]['callback'] = '_custom_background_cb';
break;
}
$_wp_theme_features[ $feature ] = $args;
}
/**
* Registers the internal custom header and background routines.
*
* @since 3.4.0
* @access private
*/
function _custom_header_background_just_in_time() {
global $custom_image_header, $custom_background;
if ( current_theme_supports( 'custom-header' ) ) {
// In case any constants were defined after an add_custom_image_header() call, re-run.
add_theme_support( 'custom-header', array( '__jit' => true ) );
$args = get_theme_support( 'custom-header' );
if ( $args[0]['callback'] )
add_action( 'wp_head', $args[0]['callback'] );
if ( is_admin() ) {
require_once( ABSPATH . 'wp-admin/custom-header.php' );
$custom_image_header = new Custom_Image_Header( $args[0]['admin-header-callback'], $args[0]['admin-image-div-callback'] );
}
}
if ( current_theme_supports( 'custom-background' ) ) {
// In case any constants were defined after an add_custom_background() call, re-run.
add_theme_support( 'custom-background', array( '__jit' => true ) );
$args = get_theme_support( 'custom-background' );
add_action( 'wp_head', $args[0]['callback'] );
if ( is_admin() ) {
require_once( ABSPATH . 'wp-admin/custom-background.php' );
$custom_background = new Custom_Background( $args[0]['admin-header-callback'], $args[0]['admin-image-div-callback'] );
}
}
}
add_action( 'wp_loaded', '_custom_header_background_just_in_time' );
/**
* Gets the theme support arguments passed when registering that support
*
@ -1439,10 +1515,24 @@ function add_theme_support( $feature ) {
*/
function get_theme_support( $feature ) {
global $_wp_theme_features;
if ( !isset( $_wp_theme_features[$feature] ) )
if ( ! isset( $_wp_theme_features[ $feature ] ) )
return false;
else
return $_wp_theme_features[$feature];
if ( func_num_args() <= 1 )
return $_wp_theme_features[ $feature ];
$args = array_slice( func_get_args(), 1 );
switch ( $feature ) {
case 'custom-header' :
case 'custom-background' :
if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
return $_wp_theme_features[ $feature ][0][ $args[0] ];
return false;
break;
default :
return $_wp_theme_features[ $feature ];
break;
}
}
/**
@ -1458,8 +1548,9 @@ function get_theme_support( $feature ) {
*/
function remove_theme_support( $feature ) {
// Blacklist: for internal registrations not used directly by themes.
if ( in_array( $feature, array( 'custom-background', 'custom-header', 'editor-style', 'widgets', 'menus' ) ) )
if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ) ) )
return false;
return _remove_theme_support( $feature );
}
@ -1472,9 +1563,35 @@ function remove_theme_support( $feature ) {
function _remove_theme_support( $feature ) {
global $_wp_theme_features;
if ( ! isset( $_wp_theme_features[$feature] ) )
switch ( $feature ) {
case 'custom-header-uploads' :
if ( ! isset( $_wp_theme_features['custom-header'] ) )
return false;
add_theme_support( 'custom-header', array( 'uploads' => false ) );
return; // Do not continue - custom-header-uploads no longer exists.
}
if ( ! isset( $_wp_theme_features[ $feature ] ) )
return false;
unset( $_wp_theme_features[$feature] );
switch ( $feature ) {
case 'custom-header' :
$support = get_theme_support( 'custom-header' );
if ( $support[0]['callback'] )
remove_action( 'wp_head', $support[0]['callback'] );
remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
unset( $GLOBALS['custom_image_header'] );
break;
case 'custom-header' :
$support = get_theme_support( 'custom-background' );
remove_action( 'wp_head', $support[0]['callback'] );
remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
unset( $GLOBALS['custom_background'] );
break;
}
unset( $_wp_theme_features[ $feature ] );
return true;
}
@ -1488,6 +1605,9 @@ function _remove_theme_support( $feature ) {
function current_theme_supports( $feature ) {
global $_wp_theme_features;
if ( 'custom-header-uploads' == $feature )
return current_theme_supports( 'custom-header', 'uploads' );
if ( !isset( $_wp_theme_features[$feature] ) )
return false;