Ticket #19840: 19840.5.2.diff

File 19840.5.2.diff, 14.9 KB (added by sabreuse, 15 months ago)

Fixes remaining notices

Line 
1Index: wp-admin/js/custom-background.dev.js
2===================================================================
3--- wp-admin/js/custom-background.dev.js        (revision 19953)
4+++ wp-admin/js/custom-background.dev.js        (working copy)
5@@ -10,6 +10,14 @@
6                jQuery('#clearcolor').hide();
7 }
8 
9+function send_to_editor( data ) {
10+       data = jQuery.parseJSON(data);
11+       jQuery.post(ajaxurl, { action: 'set-background-image', attachment_id: data.id, size: data.size }, function(){
12+               tb_remove();
13+               window.location.reload();
14+       });
15+}
16+
17 jQuery(document).ready(function() {
18        jQuery('#pickcolor').click(function() {
19                jQuery('#colorPickerDiv').show();
20Index: wp-admin/includes/media.php
21===================================================================
22--- wp-admin/includes/media.php (revision 19953)
23+++ wp-admin/includes/media.php (working copy)
24@@ -386,17 +386,26 @@
25        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>";
26 }
27 
28-function get_upload_iframe_src( $type = null ) {
29+function get_upload_iframe_src( $args = array() ) {
30        global $post_ID;
31+       $defaults = array( 'type' => '' );
32+       // Backwards compat for first parameter being type
33+       if ( is_string( $args ) )
34+               $args = array( 'type' => $args );
35 
36+       $args = wp_parse_args( $args, $defaults );
37+
38        $uploading_iframe_ID = (int) $post_ID;
39        $upload_iframe_src = add_query_arg( 'post_id', $uploading_iframe_ID, admin_url('media-upload.php') );
40 
41-       if ( $type && 'media' != $type )
42-               $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
43+       if ( $args['type'] && 'media' != $args['type'] )
44+               $upload_iframe_src = add_query_arg('type', $args['type'], $upload_iframe_src);
45 
46-       $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
47+       if ( isset($args['tab']) && $args['tab'] )
48+               $upload_iframe_src = add_query_arg('tab', $args['tab'], $upload_iframe_src);
49 
50+       $upload_iframe_src = apply_filters($args['type'] . '_upload_iframe_src', $upload_iframe_src);
51+
52        return add_query_arg('TB_iframe', true, $upload_iframe_src);
53 }
54 
55@@ -477,7 +486,7 @@
56        if ( isset($send_id) ) {
57                $attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
58 
59-               $html = $attachment['post_title'];
60+               $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
61                if ( !empty($attachment['url']) ) {
62                        $rel = '';
63                        if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
64@@ -884,13 +893,14 @@
65 function image_media_send_to_editor($html, $attachment_id, $attachment) {
66        $post =& get_post($attachment_id);
67        if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
68-               $url = $attachment['url'];
69+               $post_title = !empty($attachment['post_title']) ? $attachment['post_title'] : '';
70+               $post_excerpt = !empty($attachment['post_excerpt']) ? $attachment['post_excerpt'] : '';         $url = !empty($attachment['url']) ? $attachment['url'] : '';
71                $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
72                $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
73                $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
74                $rel = ( $url == get_attachment_link($attachment_id) );
75 
76-               return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
77+               return get_image_send_to_editor($attachment_id, $post_excerpt, $post_title, $align, $url, $rel, $size, $alt);
78        }
79 
80        return $html;
81Index: wp-admin/custom-header.php
82===================================================================
83--- wp-admin/custom-header.php  (revision 19953)
84+++ wp-admin/custom-header.php  (working copy)
85@@ -90,6 +90,10 @@
86                add_action("admin_head-$page", array(&$this, 'take_action'), 50);
87                add_action("admin_head-$page", array(&$this, 'js'), 50);
88                add_action("admin_head-$page", $this->admin_header_callback, 51);
89+
90+               add_filter('attachment_fields_to_edit', array(&$this, 'attachment_fields_to_edit'), 10, 2);
91+               add_filter('media_upload_tabs', array(&$this, 'filter_upload_tabs'));
92+               add_filter('image_send_to_editor', array(&$this, 'image_send_to_editor'), 10, 2);
93        }
94 
95        /**
96@@ -140,10 +144,13 @@
97        function js_includes() {
98                $step = $this->step();
99 
100-               if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
101-                       wp_enqueue_script('farbtastic');
102-               elseif ( 2 == $step )
103+               if ( ( 1 == $step || 3 == $step ) ) {
104+                       add_thickbox();
105+                       if ( $this->header_text() )
106+                               wp_enqueue_script('farbtastic');
107+               } elseif ( 2 == $step ) {
108                        wp_enqueue_script('imgareaselect');
109+               }
110        }
111 
112        /**
113@@ -325,10 +332,13 @@
114         */
115        function js() {
116                $step = $this->step();
117-               if ( ( 1 == $step || 3 == $step ) && $this->header_text() )
118-                       $this->js_1();
119-               elseif ( 2 == $step )
120+               if ( ( 1 == $step || 3 == $step ) ) {
121+                       $this->js_0();
122+                       if ( $this->header_text() )
123+                               $this->js_1();
124+               } elseif ( 2 == $step ) {
125                        $this->js_2();
126+               }
127        }
128 
129        /**
130@@ -336,6 +346,23 @@
131         *
132         * @since 2.6.0
133         */
134+       function js_0() { ?>
135+<script type="text/javascript">
136+/* <![CDATA[ */
137+       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') ); ?>';
138+       function send_to_editor( file_id ) {
139+               window.location = step2_url + '&file=' + file_id;
140+       }
141+/* ]]> */
142+</script>
143+<?php
144+       }
145+
146+       /**
147+        * Display Javascript based on Step 1 and 3 if header text is active.
148+        *
149+        * @since 2.6.0
150+        */
151        function js_1() { ?>
152 <script type="text/javascript">
153 /* <![CDATA[ */
154@@ -581,6 +608,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@@ -698,29 +731,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+               } else if ( 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                $header_support = get_theme_support( 'custom-header' );
205@@ -777,6 +796,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@@ -786,6 +808,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@@ -851,9 +900,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@@ -870,8 +921,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@@ -903,4 +956,29 @@
276                        $this->step_3();
277        }
278 
279+       function attachment_fields_to_edit( $form_fields, $post ) {
280+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' ) {
281+                       $form_fields = array(); // Reset
282+                       $button = get_submit_button( __( 'Use as header image' ), 'button', "send[{$post->ID}]", false );
283+                       $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td>'.$button.'</td></tr>' );
284+                       $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-header' );
285+               }
286+
287+               return $form_fields;
288+       }
289+
290+       function image_send_to_editor( $html, $id ) {
291+               if ( isset( $_REQUEST['attachments'][$id]['context'] ) && $_REQUEST['attachments'][$id]['context'] == 'custom-header' )
292+                       return $id;
293+
294+               return $html;
295+       }
296+
297+       function filter_upload_tabs( $tabs ) {
298+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' )
299+                       return array( 'library' => __('Media Library') );
300+
301+               return $tabs;
302+       }
303+
304 }
305Index: wp-admin/custom-background.php
306===================================================================
307--- wp-admin/custom-background.php      (revision 19953)
308+++ wp-admin/custom-background.php      (working copy)
309@@ -53,6 +53,7 @@
310        function __construct($admin_header_callback = '', $admin_image_div_callback = '') {
311                $this->admin_header_callback = $admin_header_callback;
312                $this->admin_image_div_callback = $admin_image_div_callback;
313+               add_action('wp_ajax_set-background-image', array($this, 'wp_set_background_image'));
314        }
315 
316        /**
317@@ -69,6 +70,9 @@
318                add_action("load-$page", array(&$this, 'admin_load'));
319                add_action("load-$page", array(&$this, 'take_action'), 49);
320                add_action("load-$page", array(&$this, 'handle_upload'), 49);
321+               add_filter('attachment_fields_to_edit', array(&$this, 'attachment_fields_to_edit'), 10, 2);
322+               add_filter('media_send_to_editor', array(&$this, 'image_send_to_editor'), 1, 3);
323+               add_filter('media_upload_tabs', array(&$this, 'filter_upload_tabs'));
324 
325                if ( $this->admin_header_callback )
326                        add_action("admin_head-$page", $this->admin_header_callback, 51);
327@@ -96,6 +100,8 @@
328                        '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
329                );
330 
331+               add_thickbox();
332+               wp_enqueue_script('media-upload');
333                wp_enqueue_script('custom-background');
334                wp_enqueue_style('farbtastic');
335        }
336@@ -246,7 +252,13 @@
337 <input type="hidden" name="action" value="save" />
338 <?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
339 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
340-</form>
341+<?php
342+       $image_library_url = get_upload_iframe_src( array( 'type' => 'image', 'tab' => 'library' ));
343+       $image_library_url = remove_query_arg('TB_iframe', $image_library_url);
344+       $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
345+?>
346+       </form>
347+       <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>
348 </td>
349 </tr>
350 </tbody>
351@@ -366,4 +378,47 @@
352                $this->updated = true;
353        }
354 
355-}
356+       function attachment_fields_to_edit( $form_fields, $post ) {
357+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) {
358+                       $form_fields = array( 'image-size' => $form_fields['image-size'] ); // Reset
359+                       $button = get_submit_button( __( 'Use as background image' ), 'button', "send[{$post->ID}]", false );
360+                       $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td>'.$button.'</td></tr>' );
361+                       $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
362+               }
363+
364+               return $form_fields;
365+       }
366+
367+       function image_send_to_editor( $html, $id, $size ) {
368+               if ( isset( $_REQUEST['attachments'][$id]['context'] ) && $_REQUEST['attachments'][$id]['context'] == 'custom-background' ) {
369+                       remove_filter('media_send_to_editor', 'image_media_send_to_editor', 10, 3);
370+                       //exit; # Note: Uncomment this line to check if any php notices are thrown.
371+                       return json_encode( array( 'id' => $id, 'size' => $size['image-size'] ) );
372+               }
373+
374+               return $html;
375+       }
376+
377+       function filter_upload_tabs ( $tabs ){
378+               if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' )
379+                       return array( 'library' => __('Media Library') );
380+
381+               return $tabs;
382+       }
383+
384+       public function wp_set_background_image() {
385+               if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
386+               $attachment_id = absint($_POST['attachment_id']);
387+               $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
388+               $size = 'thumbnail';
389+               if ( in_array( $_POST['size'], $sizes ) )
390+                       $size = esc_attr( $_POST['size'] );
391+
392+               update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
393+               $url = wp_get_attachment_image_src($attachment_id, $size);
394+               $thumbnail = wp_get_attachment_image_src($attachment_id, 'thumbnail');
395+               set_theme_mod('background_image', esc_url($url[0]));
396+               set_theme_mod('background_image_thumb', esc_url($thumbnail[0]));
397+               exit;
398+       }
399+}
400\ No newline at end of file