Skip to content

Commit b1e5b31

Browse files
author
Daryl Koopersmith
committed
Theme Customizer: Improve background image control, add correct meta key to custom headers and backgrounds uploaded using the customizer. see #19910.
git-svn-id: https://develop.svn.wordpress.org/trunk@20913 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2cd502f commit b1e5b31

4 files changed

Lines changed: 61 additions & 15 deletions

File tree

wp-admin/includes/ajax-actions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,14 @@ function wp_ajax_upload_attachment() {
16401640
wp_die();
16411641
}
16421642

1643+
if ( isset( $post_data['context'] ) && isset( $post_data['theme'] ) ) {
1644+
if ( 'custom-background' === $post_data['context'] )
1645+
update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', $post_data['theme'] );
1646+
1647+
if ( 'custom-header' === $post_data['context'] )
1648+
update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', $post_data['theme'] );
1649+
}
1650+
16431651
$post = get_post( $attachment_id );
16441652

16451653
echo json_encode( array(

wp-includes/class-wp-customize-control.php

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,52 @@ public function tab_uploaded() {
398398
<div class="uploaded-target"></div>
399399
<?php
400400
}
401+
402+
public function print_tab_image( $url, $thumbnail_url = null ) {
403+
$url = set_url_scheme( $url );
404+
$thumbnail_url = ( $thumbnail_url ) ? set_url_scheme( $thumbnail_url ) : $url;
405+
?>
406+
<a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
407+
<img src="<?php echo esc_url( $thumbnail_url ); ?>" />
408+
</a>
409+
<?php
410+
}
411+
}
412+
413+
class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
414+
public function __construct( $manager ) {
415+
parent::__construct( $manager, 'background_image', array(
416+
'label' => __( 'Background Image' ),
417+
'section' => 'background_image',
418+
'context' => 'custom-background',
419+
'get_url' => 'get_background_image',
420+
) );
421+
422+
if ( $this->setting->default )
423+
$this->add_tab( 'default', __('Default'), array( $this, 'tab_default_background' ) );
424+
}
425+
426+
public function tab_uploaded() {
427+
$backgrounds = get_posts( array(
428+
'post_type' => 'attachment',
429+
'meta_key' => '_wp_attachment_is_custom_background',
430+
'meta_value' => $this->manager->get_stylesheet(),
431+
'orderby' => 'none',
432+
'nopaging' => true,
433+
) );
434+
435+
?><div class="uploaded-target"></div><?php
436+
437+
if ( empty( $backgrounds ) )
438+
return;
439+
440+
foreach ( (array) $backgrounds as $background )
441+
$this->print_tab_image( esc_url_raw( $background->guid ) );
442+
}
443+
444+
public function tab_default_background() {
445+
$this->print_tab_image( $this->setting->default );
446+
}
401447
}
402448

403449
class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
@@ -424,21 +470,15 @@ public function tab_uploaded() {
424470

425471
?><div class="uploaded-target"></div><?php
426472

427-
foreach ( $headers as $header ) : ?>
428-
<a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $header['url'] ); ?>">
429-
<img src="<?php echo esc_url( set_url_scheme( $header['thumbnail_url'] ) ); ?>" />
430-
</a>
431-
<?php endforeach;
473+
foreach ( $headers as $header )
474+
$this->print_tab_image( $header['url'], $header['thumbnail_url'] );
432475
}
433476

434477
public function tab_default_headers() {
435478
global $custom_image_header;
436479
$custom_image_header->process_default_headers();
437480

438-
foreach ( $custom_image_header->default_headers as $header ) : ?>
439-
<a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $header['url'] ); ?>">
440-
<img src="<?php echo esc_url( set_url_scheme( $header['thumbnail_url'] ) ); ?>" />
441-
</a>
442-
<?php endforeach;
481+
foreach ( $custom_image_header->default_headers as $header )
482+
$this->print_tab_image( $header['url'], $header['thumbnail_url'] );
443483
}
444484
}

wp-includes/class-wp-customize-manager.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -698,11 +698,7 @@ public function register_controls() {
698698
'theme_supports' => 'custom-background',
699699
) );
700700

701-
$this->add_control( new WP_Customize_Image_Control( $this, 'background_image', array(
702-
'label' => __( 'Background Image' ),
703-
'section' => 'background_image',
704-
'context' => 'custom-background',
705-
) ) );
701+
$this->add_control( new WP_Customize_Background_Image_Control( $this ) );
706702

707703
$this->add_setting( 'background_repeat', array(
708704
'default' => 'repeat',

wp-includes/js/customize-controls.dev.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@
158158

159159
if ( this.params.context )
160160
control.uploader.param( 'post_data[context]', this.params.context );
161+
162+
control.uploader.param( 'post_data[theme]', api.settings.theme.stylesheet );
161163
},
162164
success: function( attachment ) {
163165
this.setting.set( attachment.url );

0 commit comments

Comments
 (0)