Ticket #19840: v2.diff

File v2.diff, 5.5 KB (added by greuben, 15 months ago)
Line 
1Index: wp-admin/includes/media.php
2===================================================================
3--- wp-admin/includes/media.php (revision 19901)
4+++ wp-admin/includes/media.php (working copy)
5@@ -386,7 +386,7 @@
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( $type = null, $tab = null ) {
11        global $post_ID;
12 
13        $uploading_iframe_ID = (int) $post_ID;
14@@ -395,6 +395,9 @@
15        if ( $type && 'media' != $type )
16                $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
17 
18+       if ( $tab )
19+               $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
20+
21        $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
22 
23        return add_query_arg('TB_iframe', true, $upload_iframe_src);
24Index: wp-admin/js/custom-background.dev.js
25===================================================================
26--- wp-admin/js/custom-background.dev.js        (revision 19901)
27+++ wp-admin/js/custom-background.dev.js        (working copy)
28@@ -10,6 +10,14 @@
29                jQuery('#clearcolor').hide();
30 }
31 
32+function send_to_editor( data ) {
33+       data = jQuery.parseJSON(data);
34+       jQuery.post(ajaxurl, { action: 'set-background-image', attachment_id: data.id, size: data.size }, function(){
35+               tb_remove();
36+               window.location.reload();
37+       });
38+}
39+
40 jQuery(document).ready(function() {
41        jQuery('#pickcolor').click(function() {
42                jQuery('#colorPickerDiv').show();
43Index: wp-admin/custom-background.php
44===================================================================
45--- wp-admin/custom-background.php      (revision 19901)
46+++ wp-admin/custom-background.php      (working copy)
47@@ -69,6 +69,9 @@
48                add_action("load-$page", array(&$this, 'admin_load'));
49                add_action("load-$page", array(&$this, 'take_action'), 49);
50                add_action("load-$page", array(&$this, 'handle_upload'), 49);
51+               add_filter('attachment_fields_to_edit', array(&$this, 'attachment_fields_to_edit'), 10, 2);
52+               add_filter('image_send_to_editor', array(&$this, 'image_send_to_editor'), 10, 7);
53+               add_filter('media_upload_tabs', array(&$this, 'filter_upload_tabs'));
54 
55                if ( $this->admin_header_callback )
56                        add_action("admin_head-$page", $this->admin_header_callback, 51);
57@@ -96,6 +99,8 @@
58                        '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
59                );
60 
61+               add_thickbox();
62+               wp_enqueue_script('media-upload');
63                wp_enqueue_script('custom-background');
64                wp_enqueue_style('farbtastic');
65        }
66@@ -246,7 +251,13 @@
67 <input type="hidden" name="action" value="save" />
68 <?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
69 <?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
70-</form>
71+<?php
72+       $image_library_url = get_upload_iframe_src('image', 'library');
73+       $image_library_url = remove_query_arg('TB_iframe', $image_library_url);
74+       $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
75+?>
76+       </form>
77+       <span class="howto"><?php _e('or'); ?></span> <a class="thickbox" href="<?php echo $image_library_url; ?>"><?php _e( 'Choose from image library' ); ?></a>
78 </td>
79 </tr>
80 </tbody>
81@@ -366,4 +377,47 @@
82                $this->updated = true;
83        }
84 
85+       function attachment_fields_to_edit( $form_fields, $post ) {
86+               if ( !isset( $_REQUEST['context'] ) ) return $form_fields;
87+               if ( $_REQUEST['context'] != 'custom-background' ) return $form_fields;
88+
89+               $form_fields = array( 'image-size' => $form_fields['image-size'] ); // Reset
90+               $button = get_submit_button( __( 'Use as background image' ), 'button', "send[{$post->ID}]", false );
91+               $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td>'.$button.'</td></tr>' );
92+               $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
93+
94+               return $form_fields;
95+       }
96+
97+       function image_send_to_editor( $html, $id, $caption = '', $title = '', $align = '', $url = '', $size ) {
98+               if ( !isset( $_REQUEST['attachments'][$id]['context'] ) ) return $html;
99+               if ( $_REQUEST['attachments'][$id]['context'] != 'custom-background' ) return $html;
100+
101+               return json_encode( array( 'id' => $id, 'size' => $size ) );
102+       }
103+
104+       function filter_upload_tabs ( $tabs ){
105+               if ( !isset( $_REQUEST['context'] ) ) return $tabs;
106+               if ( $_REQUEST['context'] != 'custom-background' ) return $tabs;
107+
108+               return array( 'library' => __('Media Library') );
109+       }
110 }
111+
112+function wp_set_background_image() {
113+       if ( ! current_user_can('edit_theme_options') ) exit;
114+       if ( ! isset( $_POST['attachment_id'] ) ) exit;
115+       $attachment_id = intval($_POST['attachment_id']);
116+       $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
117+       $size = 'thumbnail';
118+       if ( in_array( $_POST['size'], $sizes ) )
119+               $size = esc_attr( $_POST['size'] );
120+
121+       update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
122+       $url = wp_get_attachment_image_src($attachment_id);
123+       $thumbnail = wp_get_attachment_image_src($attachment_id, $size);
124+       set_theme_mod('background_image', esc_url($url[0]));
125+       set_theme_mod('background_image_thumb', esc_url($thumbnail[0]));
126+       exit;
127+}
128+add_action('wp_ajax_set-background-image', 'wp_set_background_image');
129\ No newline at end of file