Ticket #20871: 20871.3.diff

File 20871.3.diff, 12.3 KB (added by SergeyBiryukov, 11 months ago)
Line 
1Index: wp-admin/custom-background.php
2===================================================================
3--- wp-admin/custom-background.php      (revision 21032)
4+++ wp-admin/custom-background.php      (working copy)
5@@ -383,10 +383,10 @@
6                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
7                update_post_meta( $id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
8 
9-               set_theme_mod('background_image', esc_url($url));
10+               set_theme_mod('background_image', esc_url_raw($url));
11 
12                $thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' );
13-               set_theme_mod('background_image_thumb', esc_url( $thumbnail[0] ) );
14+               set_theme_mod('background_image_thumb', esc_url_raw( $thumbnail[0] ) );
15 
16                do_action('wp_create_file_in_uploads', $file, $id); // For replication
17                $this->updated = true;
18@@ -425,8 +425,8 @@
19                update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
20                $url = wp_get_attachment_image_src( $attachment_id, $size );
21                $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
22-               set_theme_mod( 'background_image', esc_url( $url[0] ) );
23-               set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) );
24+               set_theme_mod( 'background_image', esc_url_raw( $url[0] ) );
25+               set_theme_mod( 'background_image_thumb', esc_url_raw( $thumbnail[0] ) );
26                exit;
27        }
28 }
29Index: wp-admin/custom-header.php
30===================================================================
31--- wp-admin/custom-header.php  (revision 21032)
32+++ wp-admin/custom-header.php  (working copy)
33@@ -211,21 +211,7 @@
34 
35                if ( isset( $_POST['resetheader'] ) ) {
36                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
37-                       $this->process_default_headers();
38-                       $default = get_theme_support( 'custom-header', 'default-image' );
39-                       $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
40-                       foreach ( $this->default_headers as $header => $details ) {
41-                               if ( $details['url'] == $default ) {
42-                                       $default_data = $details;
43-                                       break;
44-                               }
45-                       }
46-                       set_theme_mod( 'header_image', $default );
47-                       if ( empty( $default_data['width'] ) )
48-                               $default_data['width'] = get_theme_support( 'custom-header', 'width' );
49-                       if ( empty( $default_data['height'] ) )
50-                               $default_data['height'] = get_theme_support( 'custom-header', 'height' );
51-                       set_theme_mod( 'header_image_data', (object) $default_data );
52+                       $this->reset_header_image();
53                        return;
54                }
55 
56@@ -237,7 +223,7 @@
57 
58                if ( isset( $_POST['removeheader'] ) ) {
59                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
60-                       set_theme_mod( 'header_image', 'remove-header' );
61+                       $this->remove_header_image();
62                        return;
63                }
64 
65@@ -256,25 +242,8 @@
66 
67                if ( isset( $_POST['default-header'] ) ) {
68                        check_admin_referer( 'custom-header-options', '_wpnonce-custom-header-options' );
69-                       if ( 'random-default-image' == $_POST['default-header'] ) {
70-                               set_theme_mod( 'header_image', 'random-default-image' );
71-                       } elseif ( 'random-uploaded-image' == $_POST['default-header'] ) {
72-                               set_theme_mod( 'header_image', 'random-uploaded-image' );
73-                       } else {
74-                               $this->process_default_headers();
75-                               $uploaded = get_uploaded_header_images();
76-                               if ( isset( $uploaded[$_POST['default-header']] ) ) {
77-                                       set_theme_mod( 'header_image', esc_url( $uploaded[$_POST['default-header']]['url'] ) );
78-                                       set_theme_mod( 'header_image_data', (object) $uploaded[$_POST['default-header']] );
79-                               } elseif ( isset( $this->default_headers[$_POST['default-header']] ) ) {
80-                                       set_theme_mod( 'header_image', esc_url( $this->default_headers[$_POST['default-header']]['url'] ) );
81-                                       if ( empty( $this->default_headers[$_POST['default-header']]['width'] ) )
82-                                               $this->default_headers[$_POST['default-header']]['width'] = get_theme_support( 'custom-header', 'width' );
83-                                       if ( empty( $this->default_headers[$_POST['default-header']]['height'] ) )
84-                                               $this->default_headers[$_POST['default-header']]['height'] = get_theme_support( 'custom-header', 'height' );
85-                                       set_theme_mod( 'header_image_data', (object) $this->default_headers[$_POST['default-header']] );
86-                               }
87-                       }
88+                       $this->choose_existing_header_image( $_POST['default-header'] );
89+                       return;
90                }
91        }
92 
93@@ -718,9 +687,9 @@
94                        wp_die( __( 'Cheatin’ uh?' ) );
95 
96                if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
97-                       $id = absint( $_GET['file'] );
98-                       $file = get_attached_file( $id, true );
99-                       $url = wp_get_attachment_image_src( $id, 'full');
100+                       $attachment_id = absint( $_GET['file'] );
101+                       $file = get_attached_file( $attachment_id, true );
102+                       $url = wp_get_attachment_image_src( $attachment_id, 'full');
103                        $url = $url[0];
104                } elseif ( isset( $_POST ) ) {
105                        extract($this->step_2_manage_upload());
106@@ -729,7 +698,7 @@
107                if ( file_exists( $file ) ) {
108                        list( $width, $height, $type, $attr ) = getimagesize( $file );
109                } else {
110-                       $data = wp_get_attachment_metadata( $id );
111+                       $data = wp_get_attachment_metadata( $attachment_id );
112                        $height = $data[ 'height' ];
113                        $width = $data[ 'width' ];
114                        unset( $data );
115@@ -750,19 +719,19 @@
116                {
117                        // Add the meta-data
118                        if ( file_exists( $file ) )
119-                               wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
120-                       update_post_meta( $id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
121+                               wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
122 
123-                       set_theme_mod('header_image', esc_url($url));
124-                       do_action('wp_create_file_in_uploads', $file, $id); // For replication
125+                       $this->select_new_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
126+
127+                       do_action('wp_create_file_in_uploads', $file, $attachment_id); // For replication
128                        return $this->finished();
129                } elseif ( $width > $max_width ) {
130                        $oitar = $width / $max_width;
131-                       $image = wp_crop_image($id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
132+                       $image = wp_crop_image($attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace(basename($file), 'midsize-'.basename($file), $file));
133                        if ( ! $image || is_wp_error( $image ) )
134                                wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
135 
136-                       $image = apply_filters('wp_create_file_in_uploads', $image, $id); // For replication
137+                       $image = apply_filters('wp_create_file_in_uploads', $image, $attachment_id); // For replication
138 
139                        $url = str_replace(basename($url), basename($image), $url);
140                        $width = $width / $oitar;
141@@ -788,7 +757,7 @@
142        <input type="hidden" name="y1" id="y1" value="0"/>
143        <input type="hidden" name="width" id="width" value="<?php echo esc_attr( $width ); ?>"/>
144        <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
145-       <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" />
146+       <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $attachment_id ); ?>" />
147        <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
148        <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
149        <input type="hidden" name="create-new-attachment" value="true" />
150@@ -835,8 +804,8 @@
151                );
152 
153                // Save the data
154-               $id = wp_insert_attachment( $object, $file );
155-               return compact( 'id', 'file', 'filename', 'url', 'type' );
156+               $attachment_id = wp_insert_attachment( $object, $file );
157+               return compact( 'attachment_id', 'file', 'filename', 'url', 'type' );
158        }
159 
160        /**
161@@ -918,19 +887,11 @@
162                // Update the attachment
163                $attachment_id = wp_insert_attachment( $object, $cropped );
164                wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
165-               update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option( 'stylesheet' ) );
166 
167-               set_theme_mod('header_image', $url);
168+               $width = $dst_width;
169+               $height = $dst_height;
170+               $this->select_new_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
171 
172-               $header_data                = new stdClass();
173-               $header_data->attachment_id = $attachment_id;
174-               $header_data->url           = $url;
175-               $header_data->thumbnail_url = $url;
176-               $header_data->width         = $dst_width;
177-               $header_data->height        = $dst_height;
178-
179-               set_theme_mod( 'header_image_data', $header_data );
180-
181                // cleanup
182                $medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original );
183                if ( file_exists( $medium ) )
184@@ -997,4 +958,108 @@
185                return array( 'library' => __('Media Library') );
186        }
187 
188+       /**
189+        * Remove a header image.
190+        *
191+        * @since 3.4.0
192+        */
193+       final public function remove_header_image() {
194+               set_theme_mod( 'header_image', 'remove-header' );
195+               remove_theme_mod( 'header_image_data' );
196+       }
197+
198+       /**
199+        * Reset a header image to the default image for the theme.
200+        *
201+        * This method does not do anything if the theme does not have a default header image.
202+        *
203+        * @since 3.4.0
204+        */
205+       final public function reset_header_image() {
206+               $this->process_default_headers();
207+               $default = get_theme_support( 'custom-header', 'default-image' );
208+
209+               if ( ! $default )
210+                       return;
211+
212+               $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
213+
214+               foreach ( $this->default_headers as $header => $details ) {
215+                       if ( $details['url'] == $default ) {
216+                               $default_data = $details;
217+                               break;
218+                       }
219+               }
220+
221+               if ( empty( $default_data['width'] ) )
222+                       $default_data['width'] = get_theme_support( 'custom-header', 'width' );
223+               if ( empty( $default_data['height'] ) )
224+                       $default_data['height'] = get_theme_support( 'custom-header', 'height' );
225+               set_theme_mod( 'header_image', $default );
226+               set_theme_mod( 'header_image_data', (object) $default_data );
227+       }
228+
229+       /**
230+        * Choose a header image, selected from existing uploaded and default headers.
231+        *
232+        * @param string $choice Which header image to select. Allows for values of 'random-default-image',
233+        *      for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling
234+        *      among the uploaded images; the key of a default image registered for that theme; and
235+        *      the key of an image uploaded for that theme (the basename of the URL).
236+        */
237+       final public function choose_existing_header_image( $choice ) {
238+               if ( 'random-default-image' == $choice ) {
239+                       set_theme_mod( 'header_image', 'random-default-image' );
240+                       remove_theme_mod( 'header_image_data' );
241+                       return;
242+               }
243+
244+               if ( 'random-uploaded-image' == $choice ) {
245+                       set_theme_mod( 'header_image', 'random-uploaded-image' );
246+                       remove_theme_mod( 'header_image_data' );
247+                       return;
248+               }
249+
250+               $this->process_default_headers();
251+               if ( isset( $this->default_headers[ $choice ] ) ) {
252+                       if ( empty( $this->default_headers[ $choice ]['width'] ) )
253+                               $this->default_headers[ $choice ]['width'] = get_theme_support( 'custom-header', 'width' );
254+                       if ( empty( $this->default_headers[ $choice ]['height'] ) )
255+                               $this->default_headers[ $choice ]['height'] = get_theme_support( 'custom-header', 'height' );
256+                       set_theme_mod( 'header_image', esc_url_raw( $this->default_headers[ $choice ]['url'] ) );
257+                       set_theme_mod( 'header_image_data', (object) $this->default_headers[ $choice ] );
258+                       return;
259+               }
260+
261+               $uploaded = get_uploaded_header_images();
262+               if ( isset( $uploaded[ $choice ] ) ) {
263+                       set_theme_mod( 'header_image', esc_url_raw( $uploaded[ $choice ]['url'] ) );
264+                       set_theme_mod( 'header_image_data', (object) $uploaded[ $choice ] );
265+                       return;
266+               }
267+       }
268+
269+       /**
270+        * Select a header image, either newly uploaded or from the media library.
271+        *
272+        * @since 3.4.0
273+        *
274+        * @param array $args An array of arguments: attachment_id, url, width, height. All are required.
275+        */
276+       final public function select_new_header_image( $args ) {
277+               extract( $args );
278+
279+               $url = esc_url_raw( $url );
280+
281+               $header_data                = new stdClass();
282+               $header_data->attachment_id = $attachment_id;
283+               $header_data->url           = $url;
284+               $header_data->thumbnail_url = $url;
285+               $header_data->width         = $width;
286+               $header_data->height        = $height;
287+
288+               update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
289+               set_theme_mod( 'header_image', $url );
290+               set_theme_mod( 'header_image_data', $header_data );
291+       }
292 }