Ticket #19840: 19840.6.diff

File 19840.6.diff, 13.9 KB (added by aaroncampbell, 15 months ago)
Line 
1Index: wp-admin/includes/media.php
2===================================================================
3--- wp-admin/includes/media.php (revision 19926)
4+++ wp-admin/includes/media.php (working copy)
5@@ -386,17 +386,26 @@
6        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>";
7 }
8 
9-function get_upload_iframe_src( $type = null ) {
10+function get_upload_iframe_src( $args = array() ) {
11        global $post_ID;
12+       $defaults = array( 'type' => '' );
13+       // Backwards compat for first parameter being type
14+       if ( is_string( $args ) )
15+               $args = array( 'type' => $args );
16 
17+       $args = wp_parse_args( $args, $defaults );
18+
19        $uploading_iframe_ID = (int) $post_ID;
20        $upload_iframe_src = add_query_arg( 'post_id', $uploading_iframe_ID, admin_url('media-upload.php') );
21 
22-       if ( $type && 'media' != $type )
23-               $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
24+       if ( $args['type'] && 'media' != $args['type'] )
25+               $upload_iframe_src = add_query_arg('type', $args['type'], $upload_iframe_src);
26 
27-       $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
28+       if ( ! empty( $args['tab'] ) )
29+               $upload_iframe_src = add_query_arg('tab', $args['tab'], $upload_iframe_src);
30 
31+       $upload_iframe_src = apply_filters($args['type'] . '_upload_iframe_src', $upload_iframe_src);
32+
33        return add_query_arg('TB_iframe', true, $upload_iframe_src);
34 }
35 
36@@ -477,7 +486,7 @@
37        if ( isset($send_id) ) {
38                $attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
39 
40-               $html = $attachment['post_title'];
41+               $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
42                if ( !empty($attachment['url']) ) {
43                        $rel = '';
44                        if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
45Index: wp-admin/js/custom-background.dev.js
46===================================================================
47--- wp-admin/js/custom-background.dev.js        (revision 19926)
48+++ wp-admin/js/custom-background.dev.js        (working copy)
49@@ -10,6 +10,14 @@
50                jQuery('#clearcolor').hide();
51 }
52 
53+function send_to_editor( data ) {
54+       data = jQuery.parseJSON(data);
55+       jQuery.post(ajaxurl, { action: 'set-background-image', attachment_id: data.id, size: data.size }, function(){
56+               tb_remove();
57+               window.location.reload();
58+       });
59+}
60+
61 jQuery(document).ready(function() {
62        jQuery('#pickcolor').click(function() {
63                jQuery('#colorPickerDiv').show();
64Index: wp-admin/custom-background.php
65===================================================================
66--- wp-admin/custom-background.php      (revision 19926)
67+++ wp-admin/custom-background.php      (working copy)
68@@ -53,6 +53,7 @@
69        function __construct($admin_header_callback = '', $admin_image_div_callback = '') {
70                $this->admin_header_callback = $admin_header_callback;
71                $this->admin_image_div_callback = $admin_image_div_callback;
72+               add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
73        }
74 
75        /**
76@@ -69,6 +70,9 @@
77                add_action("load-$page", array(&$this, 'admin_load'));
78                add_action("load-$page", array(&$this, 'take_action'), 49);
79                add_action("load-$page", array(&$this, 'handle_upload'), 49);
80+               add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
81+               add_filter( 'media_send_to_editor', array( $this, 'image_send_to_editor' ), 1, 3 );
82+               add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
83 
84                if ( $this->admin_header_callback )
85                        add_action("admin_head-$page", $this->admin_header_callback, 51);
86@@ -96,6 +100,8 @@
87                        '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
88                );
89 
90+               add_thickbox();
91+               wp_enqueue_script('media-upload');
92                wp_enqueue_script('custom-background');
93                wp_enqueue_style('farbtastic');
94        }
95@@ -246,7 +252,13 @@
96 <input type="hidden" name="action" value="save" />
97 <?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
98 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
99-</form>
100+<?php
101+       $image_library_url = get_upload_iframe_src( array( 'type' => 'image', 'tab' => 'library' ) );
102+       $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
103+       $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
104+?>
105+       </form>
106+       <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>
107 </td>
108 </tr>
109 </tbody>
110@@ -366,4 +378,46 @@
111                $this->updated = true;
112        }
113 
114+       function attachment_fields_to_edit( $form_fields, $post ) {
115+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) {
116+                       $form_fields = array( 'image-size' => $form_fields['image-size'] ); // Reset
117+                       $button = get_submit_button( __( 'Use as background image' ), 'button', "send[{$post->ID}]", false );
118+                       $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td>'.$button.'</td></tr>' );
119+                       $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
120+               }
121+
122+               return $form_fields;
123+       }
124+
125+       function image_send_to_editor( $html, $id, $size ) {
126+               if ( isset( $_REQUEST['attachments'][$id]['context'] ) && $_REQUEST['attachments'][$id]['context'] == 'custom-background' ) {
127+                       remove_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 );
128+                       return json_encode( array( 'id' => $id, 'size' => $size['image-size'] ) );
129+               }
130+
131+               return $html;
132+       }
133+
134+       function filter_upload_tabs ( $tabs ){
135+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' )
136+                       return array( 'library' => __('Media Library') );
137+
138+               return $tabs;
139+       }
140+
141+       public function wp_set_background_image() {
142+               if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
143+               $attachment_id = absint($_POST['attachment_id']);
144+               $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
145+               $size = 'thumbnail';
146+               if ( in_array( $_POST['size'], $sizes ) )
147+                       $size = esc_attr( $_POST['size'] );
148+
149+               update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
150+               $url = wp_get_attachment_image_src( $attachment_id, $size );
151+               $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
152+               set_theme_mod( 'background_image', esc_url( $url[0] ) );
153+               set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) );
154+               exit;
155+       }
156 }
157Index: wp-admin/custom-header.php
158===================================================================
159--- wp-admin/custom-header.php  (revision 19926)
160+++ wp-admin/custom-header.php  (working copy)
161@@ -90,6 +90,10 @@
162                add_action("admin_head-$page", array(&$this, 'take_action'), 50);
163                add_action("admin_head-$page", array(&$this, 'js'), 50);
164                add_action("admin_head-$page", $this->admin_header_callback, 51);
165+
166+               add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
167+               add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
168+               add_filter( 'media_send_to_editor', array( $this, 'image_send_to_editor' ), 1, 2 );
169        }
170 
171        /**
172@@ -140,10 +144,13 @@
173        function js_includes() {
174                $step = $this->step();
175 
176-               if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
177-                       wp_enqueue_script('farbtastic');
178-               elseif ( 2 == $step )
179+               if ( ( 1 == $step || 3 == $step ) ) {
180+                       add_thickbox();
181+                       if ( $this->header_text() )
182+                               wp_enqueue_script('farbtastic');
183+               } elseif ( 2 == $step ) {
184                        wp_enqueue_script('imgareaselect');
185+               }
186        }
187 
188        /**
189@@ -325,10 +332,13 @@
190         */
191        function js() {
192                $step = $this->step();
193-               if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
194-                       $this->js_1();
195-               elseif ( 2 == $step )
196+               if ( ( 1 == $step || 3 == $step ) ) {
197+                       $this->js_0();
198+                       if ( $this->header_text() )
199+                               $this->js_1();
200+               } elseif ( 2 == $step ) {
201                        $this->js_2();
202+               }
203        }
204 
205        /**
206@@ -336,6 +346,23 @@
207         *
208         * @since 2.6.0
209         */
210+       function js_0() { ?>
211+<script type="text/javascript">
212+/* <![CDATA[ */
213+       step2_url = '<?php echo add_query_arg( array( 'page' => 'custom-header', 'step' => 2, '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload') ), admin_url('themes.php') ); ?>';
214+       function send_to_editor( file_id ) {
215+               window.location = step2_url + '&file=' + file_id;
216+       }
217+/* ]]> */
218+</script>
219+<?php
220+       }
221+
222+       /**
223+        * Display Javascript based on Step 1 and 3 if header text is active.
224+        *
225+        * @since 2.6.0
226+        */
227        function js_1() { ?>
228 <script type="text/javascript">
229 /* <![CDATA[ */
230@@ -581,6 +608,12 @@
231                <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
232        </p>
233        </form>
234+       <?php
235+               $image_library_url = get_upload_iframe_src( array( 'type' => 'image', 'tab' => 'library' ) );
236+               $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
237+               $image_library_url = add_query_arg( array( 'context' => 'custom-header', 'TB_iframe' => 1 ), $image_library_url );
238+       ?>
239+       <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>
240 </td>
241 </tr>
242 <?php endif; ?>
243@@ -698,29 +731,15 @@
244                if ( ! current_theme_supports( 'custom-header-uploads' ) )
245                        wp_die( __( 'Cheatin&#8217; uh?' ) );
246 
247-               $overrides = array('test_form' => false);
248-               $file = wp_handle_upload($_FILES['import'], $overrides);
249+               if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
250+                       $id = absint( $_GET['file'] );
251+                       $file = get_attached_file( $id, true );
252+                       $url = wp_get_attachment_image_src( $id, 'full');
253+                       $url = $url[0];
254+               } else if ( isset( $_POST ) ) {
255+                       extract($this->step_2_manage_upload());
256+               }
257 
258-               if ( isset($file['error']) )
259-                       wp_die( $file['error'],  __( 'Image Upload Error' ) );
260-
261-               $url = $file['url'];
262-               $type = $file['type'];
263-               $file = $file['file'];
264-               $filename = basename($file);
265-
266-               // Construct the object array
267-               $object = array(
268-               'post_title' => $filename,
269-               'post_content' => $url,
270-               'post_mime_type' => $type,
271-               'guid' => $url,
272-               'context' => 'custom-header'
273-               );
274-
275-               // Save the data
276-               $id = wp_insert_attachment($object, $file);
277-
278                list($width, $height, $type, $attr) = getimagesize( $file );
279 
280                $header_support = get_theme_support( 'custom-header' );
281@@ -777,6 +796,9 @@
282        <input type="hidden" name="height" id="height" value="<?php echo esc_attr( $height ); ?>"/>
283        <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" />
284        <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" />
285+       <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?>
286+       <input type="hidden" name="new-attachment" value="true" />
287+       <?php } ?>
288        <?php wp_nonce_field( 'custom-header-crop-image' ) ?>
289 
290        <?php submit_button( __( 'Crop and Publish' ) ); ?>
291@@ -786,6 +808,33 @@
292                <?php
293        }
294 
295+
296+       function step_2_manage_upload() {
297+               $overrides = array('test_form' => false);
298+               $file = wp_handle_upload($_FILES['import'], $overrides);
299+
300+               if ( isset($file['error']) )
301+                       wp_die( $file['error'],  __( 'Image Upload Error' ) );
302+
303+               $url = $file['url'];
304+               $type = $file['type'];
305+               $file = $file['file'];
306+               $filename = basename($file);
307+
308+               // Construct the object array
309+               $object = array(
310+                       'post_title'     => $filename,
311+                       'post_content'   => $url,
312+                       'post_mime_type' => $type,
313+                       'guid'           => $url,
314+                       'context'        => 'custom-header'
315+               );
316+
317+               // Save the data
318+               $id = wp_insert_attachment( $object, $file );
319+               return compact( 'id', 'file', 'filename', 'url', 'type' );
320+       }
321+
322        /**
323         * Display third step of custom header image page.
324         *
325@@ -851,9 +900,11 @@
326                        'guid' => $url,
327                        'context' => 'custom-header'
328                );
329+               if ( isset( $_POST['new-attachment'] ) && $_POST['new-attachment'] )
330+                       unset($object['ID']);
331 
332                // Update the attachment
333-               wp_insert_attachment($object, $cropped);
334+               $attachment_id = wp_insert_attachment( $object, $cropped );
335                wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
336                update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
337 
338@@ -870,8 +921,10 @@
339 
340                // cleanup
341                $medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
342-               @unlink( apply_filters( 'wp_delete_file', $medium ) );
343-               @unlink( apply_filters( 'wp_delete_file', $original ) );
344+               if ( file_exists( $medium ) )
345+                       @unlink( apply_filters( 'wp_delete_file', $medium ) );
346+               if ( empty ( $_POST['new-attachment'] ) )
347+                       @unlink( apply_filters( 'wp_delete_file', $original ) );
348 
349                return $this->finished();
350        }
351@@ -903,4 +956,31 @@
352                        $this->step_3();
353        }
354 
355+       function attachment_fields_to_edit( $form_fields, $post ) {
356+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' ) {
357+                       $form_fields = array(); // Reset
358+                       $button = get_submit_button( __( 'Use as header image' ), 'button', "send[{$post->ID}]", false );
359+                       $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td>'.$button.'</td></tr>' );
360+                       $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-header' );
361+               }
362+
363+               return $form_fields;
364+       }
365+
366+       function image_send_to_editor( $html, $id ) {
367+               if ( isset( $_REQUEST['attachments'][$id]['context'] ) && $_REQUEST['attachments'][$id]['context'] == 'custom-header' ) {
368+                       remove_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 );
369+                       return $id;
370+               }
371+
372+               return $html;
373+       }
374+
375+       function filter_upload_tabs( $tabs ) {
376+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' )
377+                       return array( 'library' => __('Media Library') );
378+
379+               return $tabs;
380+       }
381+
382 }