Make WordPress Core

Ticket #20871: 20871.5.diff

File 20871.5.diff, 18.4 KB (added by koopersmith, 13 years ago)
  • wp-includes/class-wp-customize-manager.php

     
    756756                        'priority'       => 60,
    757757                ) );
    758758
    759                 $this->add_setting( 'header_image', array(
     759                $this->add_setting( new WP_Customize_Filter_Setting( $this, 'header_image', array(
    760760                        'default'        => get_theme_support( 'custom-header', 'default-image' ),
    761761                        'theme_supports' => 'custom-header',
    762                 ) );
     762                ) ) );
    763763
     764                $this->add_setting( new WP_Customize_Header_Image_Setting( $this, 'header_image_data', array(
     765                        // 'default'        => get_theme_support( 'custom-header', 'default-image' ),
     766                        'theme_supports' => 'custom-header',
     767                ) ) );
     768
    764769                $this->add_control( new WP_Customize_Header_Image_Control( $this ) );
    765770
    766771                /* Custom Background */
  • wp-includes/class-wp-customize-control.php

     
    1111        public $manager;
    1212        public $id;
    1313
     14        // All settings tied to the control.
    1415        public $settings;
    15         public $setting;
    1616
     17        // The primary setting for the control (if there is one).
     18        public $setting = 'default';
     19
    1720        public $priority          = 10;
    1821        public $section           = '';
    1922        public $label             = '';
     
    455458        public function __construct( $manager ) {
    456459                parent::__construct( $manager, 'header_image', array(
    457460                        'label'    => __( 'Header Image' ),
     461                        'settings' => array(
     462                                'default' => 'header_image',
     463                                'data'    => 'header_image_data',
     464                        ),
    458465                        'section'  => 'header_image',
    459466                        'context'  => 'custom-header',
    460467                        'removed'  => 'remove-header',
     
    470477                $this->add_tab( 'default',  __('Default'),  array( $this, 'tab_default_headers' ) );
    471478        }
    472479
     480        public function print_header_image( $choice, $header ) {
     481                $header['url']           = set_url_scheme( $header['url'] );
     482                $header['thumbnail_url'] = set_url_scheme( $header['thumbnail_url'] );
     483
     484                ?>
     485                <a href="#" class="thumbnail"
     486                        data-customize-image-value="<?php echo esc_url( $header['url'] ); ?>"
     487                        data-customize-header-image-data-choice="<?php echo esc_attr( $choice ); ?>"
     488                        <?php
     489                        foreach ( array( 'attachment_id', 'width', 'height', 'url', 'thumbnail_url' ) as $key ) :
     490                                if ( isset( $header[ $key ] ) ) :
     491                                        ?> data-customize-header-image-data-<?php echo esc_attr( $key ); ?>="<?php echo esc_attr( $header[ $key ] ); ?>"<?php
     492                                endif;
     493                        endforeach;
     494                        ?>
     495                        >
     496                        <img src="<?php echo esc_url( $header['thumbnail_url'] ); ?>" />
     497                </a>
     498                <?php
     499        }
     500
    473501        public function tab_uploaded() {
    474502                $headers = get_uploaded_header_images();
    475503
    476504                ?><div class="uploaded-target"></div><?php
    477505
    478                 foreach ( $headers as $header )
    479                         $this->print_tab_image( $header['url'], $header['thumbnail_url'] );
     506                foreach ( $headers as $choice => $header )
     507                        $this->print_header_image( $choice, $header );
    480508        }
    481509
    482510        public function tab_default_headers() {
    483511                global $custom_image_header;
    484512                $custom_image_header->process_default_headers();
    485513
    486                 foreach ( $custom_image_header->default_headers as $header )
    487                         $this->print_tab_image( $header['url'], $header['thumbnail_url'] );
     514                foreach ( $custom_image_header->default_headers as $choice => $header )
     515                        $this->print_header_image( $choice, $header );
    488516        }
    489517}
     518 No newline at end of file
  • wp-includes/class-wp-customize-setting.php

     
    367367                return isset( $result );
    368368        }
    369369}
     370
     371/**
     372 * A setting that is used to filter a value, but will not save the results.
     373 *
     374 * Results should be properly handled using another setting or callback.
     375 */
     376class WP_Customize_Filter_Setting extends WP_Customize_Setting {
     377        public function update() {}
     378}
     379
     380/**
     381 * A setting that is used to filter a value, but will not save the results.
     382 *
     383 * Results should be properly handled using another setting or callback.
     384 */
     385class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
     386        public $id = 'header_image_data';
     387
     388        public function update( $value ) {
     389                global $custom_image_header;
     390
     391                // If the value doesn't exist (removed or random),
     392                // use the header_image value.
     393                if ( ! $value )
     394                        $value = $this->manager->get_setting('header_image')->post_value();
     395
     396                if ( is_array( $value ) && isset( $value['choice'] ) )
     397                        $custom_image_header->set_header_image( $value['choice'] );
     398                else
     399                        $custom_image_header->set_header_image( $value );
     400        }
     401}
     402 No newline at end of file
  • wp-admin/js/customize-controls.dev.js

     
    239239                                control.selected.both.addClass('library-selected');
    240240                        });
    241241
     242                        // Bind events to switch image urls.
    242243                        this.library.on( 'click', 'a', function( event ) {
    243244                                var value = $(this).data('customizeImageValue');
    244245
     
    263264                        if ( this.tabs.uploaded && this.tabs.uploaded.target.length ) {
    264265                                this.tabs.uploaded.both.removeClass('hidden');
    265266
    266                                 $( '<a href="#" class="thumbnail"></a>' )
     267                                attachment.element = $( '<a href="#" class="thumbnail"></a>' )
    267268                                        .data( 'customizeImageValue', attachment.url )
    268269                                        .append( '<img src="' +  attachment.url+ '" />' )
    269270                                        .appendTo( this.tabs.uploaded.target );
     
    868869                        });
    869870                });
    870871
     872                // Handle header image data
     873                api.control( 'header_image', function( control ) {
     874                        control.setting.bind( function( to ) {
     875                                if ( to === control.params.removed )
     876                                        control.settings.data.set( false );
     877                        });
     878
     879                        control.library.on( 'click', 'a', function( event ) {
     880                                var el   = $(this),
     881                                        keys = [ 'attachment_id', 'url', 'width', 'height', 'thumbnail_url', 'choice' ],
     882                                        data = {};
     883
     884                                $.each( keys, function( i, key ) {
     885                                        var value = el.data( 'customize-header-image-data-' + key );
     886                                        if ( typeof value !== 'undefined' )
     887                                                data[ key ] = value;
     888                                });
     889
     890                                control.settings.data.set( data );
     891                        });
     892
     893                        control.uploader.success = function( attachment ) {
     894                                var data;
     895
     896                                api.ImageControl.prototype.success.call( control, attachment );
     897
     898                                data = {
     899                                        attachment_id: attachment.id,
     900                                        url:           attachment.url,
     901                                        thumbnail_url: attachment.url,
     902                                        height:        attachment.meta.height,
     903                                        width:         attachment.meta.width
     904                                };
     905
     906                                $.each( data, function( key, value ) {
     907                                        attachment.element.data( 'customize-header-image-data-' + key, value );
     908                                });
     909
     910                                control.settings.data.set( data );
     911                        }
     912                });
     913
    871914                api.trigger( 'ready' );
    872915        });
    873916
  • wp-admin/custom-header.php

     
    211211
    212212                if ( isset( $_POST['resetheader'] ) ) {
    213213                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
    214                         $this->process_default_headers();
    215                         $default = get_theme_support( 'custom-header', 'default-image' );
    216                         $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
    217                         foreach ( $this->default_headers as $header => $details ) {
    218                                 if ( $details['url'] == $default ) {
    219                                         $default_data = $details;
    220                                         break;
    221                                 }
    222                         }
    223                         set_theme_mod( 'header_image', $default );
    224                         if ( empty( $default_data['width'] ) )
    225                                 $default_data['width'] = get_theme_support( 'custom-header', 'width' );
    226                         if ( empty( $default_data['height'] ) )
    227                                 $default_data['height'] = get_theme_support( 'custom-header', 'height' );
    228                         set_theme_mod( 'header_image_data', (object) $default_data );
     214                        $this->reset_header_image();
    229215                        return;
    230216                }
    231217
     
    237223
    238224                if ( isset( $_POST['removeheader'] ) ) {
    239225                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
    240                         set_theme_mod( 'header_image', 'remove-header' );
     226                        $this->remove_header_image();
    241227                        return;
    242228                }
    243229
     
    256242
    257243                if ( isset( $_POST['default-header'] ) ) {
    258244                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
    259                         if ( 'random-default-image' == $_POST['default-header'] ) {
    260                                 set_theme_mod( 'header_image', 'random-default-image' );
    261                         } elseif ( 'random-uploaded-image' == $_POST['default-header'] ) {
    262                                 set_theme_mod( 'header_image', 'random-uploaded-image' );
    263                         } else {
    264                                 $this->process_default_headers();
    265                                 $uploaded = get_uploaded_header_images();
    266                                 if ( isset( $uploaded[$_POST['default-header']] ) ) {
    267                                         set_theme_mod( 'header_image', esc_url( $uploaded[$_POST['default-header']]['url'] ) );
    268                                         set_theme_mod( 'header_image_data', (object) $uploaded[$_POST['default-header']] );
    269                                 } elseif ( isset( $this->default_headers[$_POST['default-header']] ) ) {
    270                                         set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) );
    271                                         if ( empty( $this->default_headers[$_POST['default-header']]['width'] ) )
    272                                                 $this->default_headers[$_POST['default-header']]['width'] = get_theme_support( 'custom-header', 'width' );
    273                                         if ( empty( $this->default_headers[$_POST['default-header']]['height'] ) )
    274                                                 $this->default_headers[$_POST['default-header']]['height'] = get_theme_support( 'custom-header', 'height' );
    275                                         set_theme_mod( 'header_image_data', (object) $this->default_headers[$_POST['default-header']] );
    276                                 }
    277                         }
     245                        $this->set_header_image( $_POST['default-header'] );
     246                        return;
    278247                }
    279248        }
    280249
     
    718687                        wp_die( __( 'Cheatin&#8217; uh?' ) );
    719688
    720689                if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
    721                         $id = absint( $_GET['file'] );
    722                         $file = get_attached_file( $id, true );
    723                         $url = wp_get_attachment_image_src( $id, 'full');
     690                        $attachment_id = absint( $_GET['file'] );
     691                        $file = get_attached_file( $attachment_id, true );
     692                        $url = wp_get_attachment_image_src( $attachment_id, 'full');
    724693                        $url = $url[0];
    725694                } elseif ( isset( $_POST ) ) {
    726695                        extract($this->step_2_manage_upload());
     
    729698                if ( file_exists( $file ) ) {
    730699                        list( $width, $height, $type, $attr ) = getimagesize( $file );
    731700                } else {
    732                         $data = wp_get_attachment_metadata( $id );
     701                        $data = wp_get_attachment_metadata( $attachment_id );
    733702                        $height = $data[ 'height' ];
    734703                        $width = $data[ 'width' ];
    735704                        unset( $data );
     
    750719                {
    751720                        // Add the meta-data
    752721                        if ( file_exists( $file ) )
    753                                 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    754                         update_post_meta( $id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
     722                                wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
    755723
    756                         set_theme_mod('header_image', esc_url($url));
    757                         do_action('wp_create_file_in_uploads', $file, $id); // For replication
     724                        $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
     725
     726                        do_action('wp_create_file_in_uploads', $file, $attachment_id); // For replication
    758727                        return $this->finished();
    759728                } elseif ( $width > $max_width ) {
    760729                        $oitar = $width / $max_width;
    761                         $image = wp_crop_image($id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
     730                        $image = wp_crop_image($attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
    762731                        if ( ! $image || is_wp_error( $image ) )
    763732                                wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    764733
    765                         $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
     734                        $image = apply_filters('wp_create_file_in_uploads', $image, $attachment_id); // For replication
    766735
    767736                        $url = str_replace(basename($url), basename($image), $url);
    768737                        $width = $width / $oitar;
     
    788757        <input type="hidden" name="y1" id="y1" value="0"/>
    789758        <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
    790759        <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
    791         <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" />
     760        <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
    792761        <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
    793762        <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
    794763        <input type="hidden" name="create-new-attachment" value="true" />
     
    835804                );
    836805
    837806                // Save the data
    838                 $id = wp_insert_attachment( $object, $file );
    839                 return compact( 'id', 'file', 'filename', 'url', 'type' );
     807                $attachment_id = wp_insert_attachment( $object, $file );
     808                return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
    840809        }
    841810
    842811        /**
     
    918887                // Update the attachment
    919888                $attachment_id = wp_insert_attachment( $object, $cropped );
    920889                wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
    921                 update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option( 'stylesheet' ) );
    922890
    923                 set_theme_mod('header_image', $url);
     891                $width = $dst_width;
     892                $height = $dst_height;
     893                $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
    924894
    925                 $header_data                = new stdClass();
    926                 $header_data->attachment_id = $attachment_id;
    927                 $header_data->url           = $url;
    928                 $header_data->thumbnail_url = $url;
    929                 $header_data->width         = $dst_width;
    930                 $header_data->height        = $dst_height;
    931 
    932                 set_theme_mod( 'header_image_data', $header_data );
    933 
    934895                // cleanup
    935896                $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original );
    936897                if ( file_exists( $medium ) )
     
    997958                return array( 'library' => __('Media Library') );
    998959        }
    999960
     961        /**
     962         * Choose a header image, selected from existing uploaded and default headers,
     963         * or provide an array of uploaded header data (either new, or from media library).
     964         *
     965         * @param mixed $choice Which header image to select. Allows for values of 'random-default-image',
     966         *      for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling
     967         *      among the uploaded images; the key of a default image registered for that theme; and
     968         *      the key of an image uploaded for that theme (the basename of the URL).
     969         *  Or an array of arguments: attachment_id, url, width, height. All are required.
     970         *
     971         * @since 3.4.0
     972         */
     973        final public function set_header_image( $choice ) {
     974                if ( is_array( $choice ) || is_object( $choice ) ) {
     975                        $choice = (array) $choice;
     976                        if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) )
     977                                return;
     978
     979                        $choice['url'] = esc_url_raw( $choice['url'] );
     980
     981                        $header_image_data = (object) array(
     982                                'attachment_id' => $choice['attachment_id'],
     983                                'url'           => $choice['url'],
     984                                'thumbnail_url' => $choice['url'],
     985                                'height'        => $choice['height'],
     986                                'width'         => $choice['width'],
     987                        );
     988
     989                        update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() );
     990                        set_theme_mod( 'header_image', $choice['url'] );
     991                        set_theme_mod( 'header_image_data', $header_image_data );
     992                        return;
     993                }
     994
     995                if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ) ) ) {
     996                        set_theme_mod( 'header_image', $choice );
     997                        remove_theme_mod( 'header_image_data' );
     998                        return;
     999                }
     1000
     1001                $uploaded = get_uploaded_header_images();
     1002                if ( $uploaded && isset( $uploaded[ $choice ] ) ) {
     1003                        $header_image_data = $uploaded[ $choice ];
     1004
     1005                } else {
     1006                        $this->process_default_headers();
     1007                        if ( isset( $this->default_headers[ $choice ] ) )
     1008                                $header_image_data = $this->default_headers[ $choice ];
     1009                        else
     1010                                return;
     1011                }
     1012
     1013                set_theme_mod( 'header_image', esc_url_raw( $header_image_data['url'] ) );
     1014                set_theme_mod( 'header_image_data', $header_image_data );
     1015        }
     1016
     1017        /**
     1018         * Remove a header image.
     1019         *
     1020         * @since 3.4.0
     1021         */
     1022        final public function remove_header_image() {
     1023                return $this->set_header_image( 'remove-header' );
     1024        }
     1025
     1026        /**
     1027         * Reset a header image to the default image for the theme.
     1028         *
     1029         * This method does not do anything if the theme does not have a default header image.
     1030         *
     1031         * @since 3.4.0
     1032         */
     1033        final public function reset_header_image() {
     1034                $this->process_default_headers();
     1035                $default = get_theme_support( 'custom-header', 'default-image' );
     1036
     1037                if ( ! $default )
     1038                        return $this->remove_header_image();
     1039
     1040                $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1041
     1042                foreach ( $this->default_headers as $header => $details ) {
     1043                        if ( $details['url'] == $default ) {
     1044                                $default_data = $details;
     1045                                break;
     1046                        }
     1047                }
     1048
     1049                set_theme_mod( 'header_image', $default );
     1050                set_theme_mod( 'header_image_data', (object) $default_data );
     1051        }
    10001052}
  • wp-admin/custom-background.php

     
    383383                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
    384384                update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
    385385
    386                 set_theme_mod('background_image', esc_url($url));
     386                set_theme_mod('background_image', esc_url_raw($url));
    387387
    388388                $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
    389                 set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) );
     389                set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
    390390
    391391                do_action('wp_create_file_in_uploads', $file, $id); // For replication
    392392                $this->updated = true;
     
    425425                update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
    426426                $url = wp_get_attachment_image_src( $attachment_id, $size );
    427427                $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
    428                 set_theme_mod( 'background_image', esc_url( $url[0] ) );
    429                 set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) );
     428                set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
     429                set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
    430430                exit;
    431431        }
    432432}