WordPress.org

Make WordPress Core

Ticket #19840: v2.diff

File v2.diff, 5.5 KB (added by greuben, 2 years ago)
  • wp-admin/includes/media.php

     
    386386        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>"; 
    387387} 
    388388 
    389 function get_upload_iframe_src( $type = null ) { 
     389function get_upload_iframe_src( $type = null, $tab = null ) { 
    390390        global $post_ID; 
    391391 
    392392        $uploading_iframe_ID = (int) $post_ID; 
     
    395395        if ( $type && 'media' != $type ) 
    396396                $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src); 
    397397 
     398        if ( $tab ) 
     399                $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src); 
     400 
    398401        $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src); 
    399402 
    400403        return add_query_arg('TB_iframe', true, $upload_iframe_src); 
  • wp-admin/js/custom-background.dev.js

     
    1010                jQuery('#clearcolor').hide(); 
    1111} 
    1212 
     13function send_to_editor( data ) { 
     14        data = jQuery.parseJSON(data); 
     15        jQuery.post(ajaxurl, { action: 'set-background-image', attachment_id: data.id, size: data.size }, function(){ 
     16                tb_remove(); 
     17                window.location.reload(); 
     18        }); 
     19} 
     20 
    1321jQuery(document).ready(function() { 
    1422        jQuery('#pickcolor').click(function() { 
    1523                jQuery('#colorPickerDiv').show(); 
  • wp-admin/custom-background.php

     
    6969                add_action("load-$page", array(&$this, 'admin_load')); 
    7070                add_action("load-$page", array(&$this, 'take_action'), 49); 
    7171                add_action("load-$page", array(&$this, 'handle_upload'), 49); 
     72                add_filter('attachment_fields_to_edit', array(&$this, 'attachment_fields_to_edit'), 10, 2); 
     73                add_filter('image_send_to_editor', array(&$this, 'image_send_to_editor'), 10, 7); 
     74                add_filter('media_upload_tabs', array(&$this, 'filter_upload_tabs')); 
    7275 
    7376                if ( $this->admin_header_callback ) 
    7477                        add_action("admin_head-$page", $this->admin_header_callback, 51); 
     
    9699                        '<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>' 
    97100                ); 
    98101 
     102                add_thickbox(); 
     103                wp_enqueue_script('media-upload'); 
    99104                wp_enqueue_script('custom-background'); 
    100105                wp_enqueue_style('farbtastic'); 
    101106        } 
     
    246251<input type="hidden" name="action" value="save" /> 
    247252<?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?> 
    248253<?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?> 
    249 </form> 
     254<?php 
     255        $image_library_url = get_upload_iframe_src('image', 'library'); 
     256        $image_library_url = remove_query_arg('TB_iframe', $image_library_url); 
     257        $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url ); 
     258?> 
     259        </form> 
     260        <span class="howto"><?php _e('or'); ?></span> <a class="thickbox" href="<?php echo $image_library_url; ?>"><?php _e( 'Choose from image library' ); ?></a> 
    250261</td> 
    251262</tr> 
    252263</tbody> 
     
    366377                $this->updated = true; 
    367378        } 
    368379 
     380        function attachment_fields_to_edit( $form_fields, $post ) { 
     381                if ( !isset( $_REQUEST['context'] ) ) return $form_fields; 
     382                if ( $_REQUEST['context'] != 'custom-background' ) return $form_fields; 
     383 
     384                $form_fields = array( 'image-size' => $form_fields['image-size'] ); // Reset 
     385                $button = get_submit_button( __( 'Use as background image' ), 'button', "send[{$post->ID}]", false ); 
     386                $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td>'.$button.'</td></tr>' ); 
     387                $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' ); 
     388 
     389                return $form_fields; 
     390        } 
     391 
     392        function image_send_to_editor( $html, $id, $caption = '', $title = '', $align = '', $url = '', $size ) { 
     393                if ( !isset( $_REQUEST['attachments'][$id]['context'] ) ) return $html; 
     394                if ( $_REQUEST['attachments'][$id]['context'] != 'custom-background' ) return $html; 
     395 
     396                return json_encode( array( 'id' => $id, 'size' => $size ) ); 
     397        } 
     398 
     399        function filter_upload_tabs ( $tabs ){ 
     400                if ( !isset( $_REQUEST['context'] ) ) return $tabs; 
     401                if ( $_REQUEST['context'] != 'custom-background' ) return $tabs; 
     402 
     403                return array( 'library' => __('Media Library') ); 
     404        } 
    369405} 
     406 
     407function wp_set_background_image() { 
     408        if ( ! current_user_can('edit_theme_options') ) exit; 
     409        if ( ! isset( $_POST['attachment_id'] ) ) exit; 
     410        $attachment_id = intval($_POST['attachment_id']); 
     411        $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) )); 
     412        $size = 'thumbnail'; 
     413        if ( in_array( $_POST['size'], $sizes ) ) 
     414                $size = esc_attr( $_POST['size'] ); 
     415 
     416        update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) ); 
     417        $url = wp_get_attachment_image_src($attachment_id); 
     418        $thumbnail = wp_get_attachment_image_src($attachment_id, $size); 
     419        set_theme_mod('background_image', esc_url($url[0])); 
     420        set_theme_mod('background_image_thumb', esc_url($thumbnail[0])); 
     421        exit; 
     422} 
     423add_action('wp_ajax_set-background-image', 'wp_set_background_image'); 
     424 No newline at end of file