Ticket #19840: 19840.14.diff

File 19840.14.diff, 15.1 KB (added by aaroncampbell, 14 months ago)
Line 
1Index: wp-includes/script-loader.php
2===================================================================
3--- wp-includes/script-loader.php       (revision 20289)
4+++ wp-includes/script-loader.php       (working copy)
5@@ -419,6 +419,8 @@
6                ) );
7 
8                $scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array('farbtastic'), false, 1 );
9+
10+               $scripts->add( 'custom-header', "/wp-admin/js/custom-header$suffix.js", array(), false, 1 );
11        }
12 }
13 
14Index: wp-admin/includes/post.php
15===================================================================
16--- wp-admin/includes/post.php  (revision 20289)
17+++ wp-admin/includes/post.php  (working copy)
18@@ -1152,7 +1152,7 @@
19        if ( empty( $post_id ) )
20                $post_id = $post_ID;
21 
22-       $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="' . esc_url( get_upload_iframe_src('image', $post_id) ) . '" id="set-post-thumbnail" class="thickbox">%s</a></p>';
23+       $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="' . esc_url( get_upload_iframe_src( array( 'type'=>'image', 'post_id'=>$post_id ) ) ) . '" id="set-post-thumbnail" class="thickbox">%s</a></p>';
24        $content = sprintf($set_thumbnail_link, esc_html__( 'Set featured image' ));
25 
26        if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
27Index: wp-admin/includes/media.php
28===================================================================
29--- wp-admin/includes/media.php (revision 20289)
30+++ wp-admin/includes/media.php (working copy)
31@@ -403,20 +403,29 @@
32        return "<a href='" . esc_url( get_upload_iframe_src($type) ) . "' id='{$id}-add_{$type}' class='thickbox add_$type' title='" . esc_attr( $title ) . "'><img src='" . esc_url( admin_url( $icon ) ) . "' alt='$title' onclick='return false;' /></a>";
33 }
34 
35-function get_upload_iframe_src( $type = null, $post_id = null ) {
36+function get_upload_iframe_src( $args = array() ) {
37        global $post_ID;
38 
39-       if ( empty( $post_id ) )
40-               $post_id = $post_ID;
41+       $defaults = array( 'type' => '', 'post_id' => $post_ID );
42+       // Backwards compat for first parameter being type
43+       if ( is_string( $args ) )
44+               $args = array(
45+                       'type' => $args,
46+                       'post_id'=> $post_ID
47+               );
48 
49-       $uploading_iframe_ID = (int) $post_id;
50-       $upload_iframe_src = add_query_arg( 'post_id', $uploading_iframe_ID, admin_url('media-upload.php') );
51+       $args = wp_parse_args( $args, $defaults );
52 
53-       if ( $type && 'media' != $type )
54-               $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
55+       $upload_iframe_src = add_query_arg( 'post_id', (int) $args['post_id'], admin_url('media-upload.php') );
56 
57-       $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
58+       if ( $args['type'] && 'media' != $args['type'] )
59+               $upload_iframe_src = add_query_arg('type', $args['type'], $upload_iframe_src);
60 
61+       if ( ! empty( $args['tab'] ) )
62+               $upload_iframe_src = add_query_arg('tab', $args['tab'], $upload_iframe_src);
63+
64+       $upload_iframe_src = apply_filters($args['type'] . '_upload_iframe_src', $upload_iframe_src);
65+
66        return add_query_arg('TB_iframe', true, $upload_iframe_src);
67 }
68 
69@@ -497,7 +506,7 @@
70        if ( isset($send_id) ) {
71                $attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
72 
73-               $html = $attachment['post_title'];
74+               $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
75                if ( !empty($attachment['url']) ) {
76                        $rel = '';
77                        if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
78Index: wp-admin/js/custom-background.dev.js
79===================================================================
80--- wp-admin/js/custom-background.dev.js        (revision 20289)
81+++ wp-admin/js/custom-background.dev.js        (working copy)
82@@ -53,3 +53,15 @@
83                });
84        });
85 });
86+
87+function WPSetAsBackground(id, img_size){
88+       jQuery.post(ajaxurl, {
89+               action: 'set-background-image',
90+               attachment_id: id,
91+               size: img_size
92+       }, function(){
93+               win = window.dialogArguments || opener || parent || top;
94+               win.tb_remove();
95+               win.location.reload();
96+       });
97+}
98Index: wp-admin/custom-header.php
99===================================================================
100--- wp-admin/custom-header.php  (revision 20289)
101+++ wp-admin/custom-header.php  (working copy)
102@@ -92,6 +92,9 @@
103                add_action("admin_head-$page", array(&$this, 'take_action'), 50);
104                add_action("admin_head-$page", array(&$this, 'js'), 50);
105                add_action("admin_head-$page", $this->admin_header_callback, 51);
106+
107+               add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
108+               add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
109        }
110 
111        /**
112@@ -128,8 +131,11 @@
113                        return 1;
114 
115                $step = (int) $_GET['step'];
116-               if ( $step < 1 || 3 < $step )
117-                       $step = 1;
118+               if ( $step < 1 || 3 < $step ||
119+                       ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
120+                       ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
121+               )
122+                       return 1;
123 
124                return $step;
125        }
126@@ -142,10 +148,15 @@
127        function js_includes() {
128                $step = $this->step();
129 
130-               if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
131-                       wp_enqueue_script('farbtastic');
132-               elseif ( 2 == $step )
133+               if ( ( 1 == $step || 3 == $step ) ) {
134+                       add_thickbox();
135+                       wp_enqueue_script( 'media-upload' );
136+                       wp_enqueue_script( 'custom-header' );
137+                       if ( current_theme_supports( 'custom-header', 'header-text' ) )
138+                               wp_enqueue_script('farbtastic');
139+               } elseif ( 2 == $step ) {
140                        wp_enqueue_script('imgareaselect');
141+               }
142        }
143 
144        /**
145@@ -395,7 +406,7 @@
146                <?php if ( display_header_text() ) { ?>
147                pickColor('#<?php echo get_header_textcolor(); ?>');
148                <?php } else { ?>
149-               toggle_text();         
150+               toggle_text();
151                <?php } ?>
152        });
153 })(jQuery);
154@@ -553,6 +564,12 @@
155                <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
156        </p>
157        </form>
158+       <?php
159+               $image_library_url = get_upload_iframe_src( array( 'type' => 'image', 'tab' => 'library' ) );
160+               $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
161+               $image_library_url = add_query_arg( array( 'context' => 'custom-header', 'TB_iframe' => 1 ), $image_library_url );
162+       ?>
163+       <span class="howto"><?php _ex( 'or', 'Custom Header: Choose an image from your computer - or - Choose from image library' ); ?></span> <a class="thickbox" href="<?php echo $image_library_url; ?>"><?php _e( 'Choose from image library' ); ?></a>
164 </td>
165 </tr>
166 <?php endif; ?>
167@@ -674,29 +691,15 @@
168                if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
169                        wp_die( __( 'Cheatin&#8217; uh?' ) );
170 
171-               $overrides = array('test_form' => false);
172-               $file = wp_handle_upload($_FILES['import'], $overrides);
173+               if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
174+                       $id = absint( $_GET['file'] );
175+                       $file = get_attached_file( $id, true );
176+                       $url = wp_get_attachment_image_src( $id, 'full');
177+                       $url = $url[0];
178+               } elseif ( isset( $_POST ) ) {
179+                       extract($this->step_2_manage_upload());
180+               }
181 
182-               if ( isset($file['error']) )
183-                       wp_die( $file['error'],  __( 'Image Upload Error' ) );
184-
185-               $url = $file['url'];
186-               $type = $file['type'];
187-               $file = $file['file'];
188-               $filename = basename($file);
189-
190-               // Construct the object array
191-               $object = array(
192-               'post_title' => $filename,
193-               'post_content' => $url,
194-               'post_mime_type' => $type,
195-               'guid' => $url,
196-               'context' => 'custom-header'
197-               );
198-
199-               // Save the data
200-               $id = wp_insert_attachment($object, $file);
201-
202                list($width, $height, $type, $attr) = getimagesize( $file );
203 
204                $max_width = 0;
205@@ -753,6 +756,9 @@
206        <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
207        <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" />
208        <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
209+       <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
210+       <input type="hidden" name="new-attachment" value="true" />
211+       <?php } ?>
212        <?php wp_nonce_field( 'custom-header-crop-image' ) ?>
213 
214        <?php submit_button( __( 'Crop and Publish' ) ); ?>
215@@ -762,6 +768,33 @@
216                <?php
217        }
218 
219+
220+       function step_2_manage_upload() {
221+               $overrides = array('test_form' => false);
222+               $file = wp_handle_upload($_FILES['import'], $overrides);
223+
224+               if ( isset($file['error']) )
225+                       wp_die( $file['error'],  __( 'Image Upload Error' ) );
226+
227+               $url = $file['url'];
228+               $type = $file['type'];
229+               $file = $file['file'];
230+               $filename = basename($file);
231+
232+               // Construct the object array
233+               $object = array(
234+                       'post_title'     => $filename,
235+                       'post_content'   => $url,
236+                       'post_mime_type' => $type,
237+                       'guid'           => $url,
238+                       'context'        => 'custom-header'
239+               );
240+
241+               // Save the data
242+               $id = wp_insert_attachment( $object, $file );
243+               return compact( 'id', 'file', 'filename', 'url', 'type' );
244+       }
245+
246        /**
247         * Display third step of custom header image page.
248         *
249@@ -826,9 +859,11 @@
250                        'guid' => $url,
251                        'context' => 'custom-header'
252                );
253+               if ( isset( $_POST['new-attachment'] ) && $_POST['new-attachment'] )
254+                       unset($object['ID']);
255 
256                // Update the attachment
257-               wp_insert_attachment($object, $cropped);
258+               $attachment_id = wp_insert_attachment( $object, $cropped );
259                wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
260                update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
261 
262@@ -845,8 +880,10 @@
263 
264                // cleanup
265                $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
266-               @unlink( apply_filters( 'wp_delete_file', $medium ) );
267-               @unlink( apply_filters( 'wp_delete_file', $original ) );
268+               if ( file_exists( $medium ) )
269+                       @unlink( apply_filters( 'wp_delete_file', $medium ) );
270+               if ( empty ( $_POST['new-attachment'] ) )
271+                       @unlink( apply_filters( 'wp_delete_file', $original ) );
272 
273                return $this->finished();
274        }
275@@ -870,12 +907,36 @@
276                if ( ! current_user_can('edit_theme_options') )
277                        wp_die(__('You do not have permission to customize headers.'));
278                $step = $this->step();
279-               if ( 1 == $step || ! $_POST )
280-                       $this->step_1();
281-               elseif ( 2 == $step )
282+               if ( 2 == $step )
283                        $this->step_2();
284                elseif ( 3 == $step )
285                        $this->step_3();
286+               else
287+                       $this->step_1();
288        }
289 
290+       function attachment_fields_to_edit( $form_fields, $post ) {
291+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' ) {
292+                       $form_fields = array();
293+                       $href = rawurlencode(add_query_arg(array(
294+                               'page' => 'custom-header',
295+                               'step' => 2,
296+                               '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'),
297+                               'file' => $post->ID
298+                       ), admin_url('themes.php')));
299+
300+                       $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td><a href="javascript: ( window.dialogArguments || opener || parent || top ).location.href=\'' . $href . '\';">' . _( 'Set as header' ) . '</a></td></tr>' );
301+                       $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-header' );
302+               }
303+
304+               return $form_fields;
305+       }
306+
307+       function filter_upload_tabs( $tabs ) {
308+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' )
309+                       return array( 'library' => __('Media Library') );
310+
311+               return $tabs;
312+       }
313+
314 }
315Index: wp-admin/custom-background.php
316===================================================================
317--- wp-admin/custom-background.php      (revision 20289)
318+++ wp-admin/custom-background.php      (working copy)
319@@ -55,6 +55,7 @@
320                $this->admin_image_div_callback = $admin_image_div_callback;
321 
322                add_action( 'admin_menu', array( $this, 'init' ) );
323+               add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
324        }
325 
326        /**
327@@ -71,6 +72,8 @@
328                add_action("load-$page", array(&$this, 'admin_load'));
329                add_action("load-$page", array(&$this, 'take_action'), 49);
330                add_action("load-$page", array(&$this, 'handle_upload'), 49);
331+               add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
332+               add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
333 
334                if ( $this->admin_header_callback )
335                        add_action("admin_head-$page", $this->admin_header_callback, 51);
336@@ -98,6 +101,8 @@
337                        '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
338                );
339 
340+               add_thickbox();
341+               wp_enqueue_script('media-upload');
342                wp_enqueue_script('custom-background');
343                wp_enqueue_style('farbtastic');
344        }
345@@ -126,6 +131,7 @@
346                        set_theme_mod('background_image', '');
347                        set_theme_mod('background_image_thumb', '');
348                        $this->updated = true;
349+                       wp_safe_redirect( $_POST['_wp_http_referer'] );
350                        return;
351                }
352 
353@@ -248,7 +254,13 @@
354 <input type="hidden" name="action" value="save" />
355 <?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
356 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
357-</form>
358+<?php
359+       $image_library_url = get_upload_iframe_src( array( 'type' => 'image', 'tab' => 'library' ) );
360+       $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
361+       $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
362+?>
363+       </form>
364+       <span class="howto"><?php _ex( 'or', 'Custom Background: Choose an image from your computer - or - Choose from image library' ); ?></span> <a class="thickbox" href="<?php echo $image_library_url; ?>"><?php _e( 'Choose from image library' ); ?></a>
365 </td>
366 </tr>
367 </tbody>
368@@ -368,4 +380,36 @@
369                $this->updated = true;
370        }
371 
372+       function attachment_fields_to_edit( $form_fields, $post ) {
373+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) {
374+                       $form_fields = array( 'image-size' => $form_fields['image-size'] );
375+                       $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td><a href="javascript: ( window.dialogArguments || opener || parent || top ).WPSetAsBackground(' . $post->ID . ', jQuery(\'input[name=\\\'attachments[' . $post->ID . '][image-size]\\\']\').filter(\':checked\').val());">' . _( 'Set as background' ) . '</a></td></tr>' );
376+                       $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
377+               }
378+
379+               return $form_fields;
380+       }
381+
382+       function filter_upload_tabs ( $tabs ){
383+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' )
384+                       return array( 'library' => __('Media Library') );
385+
386+               return $tabs;
387+       }
388+
389+       public function wp_set_background_image() {
390+               if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
391+               $attachment_id = absint($_POST['attachment_id']);
392+               $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
393+               $size = 'thumbnail';
394+               if ( in_array( $_POST['size'], $sizes ) )
395+                       $size = esc_attr( $_POST['size'] );
396+
397+               update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
398+               $url = wp_get_attachment_image_src( $attachment_id, $size );
399+               $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
400+               set_theme_mod( 'background_image', esc_url( $url[0] ) );
401+               set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) );
402+               exit;
403+       }
404 }