WordPress.org

Make WordPress Core

Changeset 20358


Ignore:
Timestamp:
04/05/12 00:20:28 (2 years ago)
Author:
ryan
Message:

Allow selecting custom header and background images from the media library. Props aaroncampbell, sabreuse, greuben. fixes #19840

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/custom-background.php

    r20212 r20358  
    5656 
    5757        add_action( 'admin_menu', array( $this, 'init' ) ); 
     58        add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) ); 
    5859    } 
    5960 
     
    7273        add_action("load-$page", array(&$this, 'take_action'), 49); 
    7374        add_action("load-$page", array(&$this, 'handle_upload'), 49); 
     75        add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 ); 
     76        add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) ); 
    7477 
    7578        if ( $this->admin_header_callback ) 
     
    99102        ); 
    100103 
     104        add_thickbox(); 
     105        wp_enqueue_script('media-upload'); 
    101106        wp_enqueue_script('custom-background'); 
    102107        wp_enqueue_style('farbtastic'); 
     
    127132            set_theme_mod('background_image_thumb', ''); 
    128133            $this->updated = true; 
     134            wp_safe_redirect( $_POST['_wp_http_referer'] ); 
    129135            return; 
    130136        } 
     
    249255<?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?> 
    250256<?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?> 
    251 </form> 
     257<?php 
     258    $image_library_url = get_upload_iframe_src( 'image', null, 'library' ); 
     259    $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url ); 
     260    $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url ); 
     261?> 
     262    </form> 
     263    <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> 
    252264</td> 
    253265</tr> 
     
    369381    } 
    370382 
     383    function attachment_fields_to_edit( $form_fields, $post ) { 
     384        if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) { 
     385            $form_fields = array( 'image-size' => $form_fields['image-size'] ); 
     386            $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td><a data-attachment-id="' . $post->ID . '" class="wp-set-background">' . _( 'Set as background' ) . '</a></td></tr>' ); 
     387            $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' ); 
     388        } 
     389 
     390        return $form_fields; 
     391    } 
     392 
     393    function filter_upload_tabs ( $tabs ){ 
     394        if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) 
     395            return array( 'library' => __('Media Library') ); 
     396 
     397        return $tabs; 
     398    } 
     399 
     400    public function wp_set_background_image() { 
     401        if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit; 
     402        $attachment_id = absint($_POST['attachment_id']); 
     403        $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) )); 
     404        $size = 'thumbnail'; 
     405        if ( in_array( $_POST['size'], $sizes ) ) 
     406            $size = esc_attr( $_POST['size'] ); 
     407 
     408        update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) ); 
     409        $url = wp_get_attachment_image_src( $attachment_id, $size ); 
     410        $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' ); 
     411        set_theme_mod( 'background_image', esc_url( $url[0] ) ); 
     412        set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) ); 
     413        exit; 
     414    } 
    371415} 
  • trunk/wp-admin/custom-header.php

    r20243 r20358  
    9393        add_action("admin_head-$page", array(&$this, 'js'), 50); 
    9494        add_action("admin_head-$page", $this->admin_header_callback, 51); 
     95 
     96        add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 ); 
     97        add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) ); 
    9598    } 
    9699 
     
    129132 
    130133        $step = (int) $_GET['step']; 
    131         if ( $step < 1 || 3 < $step ) 
    132             $step = 1; 
     134        if ( $step < 1 || 3 < $step || 
     135            ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) || 
     136            ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) ) 
     137        ) 
     138            return 1; 
    133139 
    134140        return $step; 
     
    143149        $step = $this->step(); 
    144150 
    145         if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) ) 
    146             wp_enqueue_script('farbtastic'); 
    147         elseif ( 2 == $step ) 
     151        if ( ( 1 == $step || 3 == $step ) ) { 
     152            add_thickbox(); 
     153            wp_enqueue_script( 'media-upload' ); 
     154            wp_enqueue_script( 'custom-header' ); 
     155            if ( current_theme_supports( 'custom-header', 'header-text' ) ) 
     156                wp_enqueue_script('farbtastic'); 
     157        } elseif ( 2 == $step ) { 
    148158            wp_enqueue_script('imgareaselect'); 
     159        } 
    149160    } 
    150161 
     
    396407        pickColor('#<?php echo get_header_textcolor(); ?>'); 
    397408        <?php } else { ?> 
    398         toggle_text();       
     409        toggle_text(); 
    399410        <?php } ?> 
    400411    }); 
     
    554565    </p> 
    555566    </form> 
     567    <?php 
     568        $image_library_url = get_upload_iframe_src( 'image', null, 'library' ); 
     569        $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url ); 
     570        $image_library_url = add_query_arg( array( 'context' => 'custom-header', 'TB_iframe' => 1 ), $image_library_url ); 
     571    ?> 
     572    <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> 
    556573</td> 
    557574</tr> 
     
    675692            wp_die( __( 'Cheatin&#8217; uh?' ) ); 
    676693 
    677         $overrides = array('test_form' => false); 
    678         $file = wp_handle_upload($_FILES['import'], $overrides); 
    679  
    680         if ( isset($file['error']) ) 
    681             wp_die( $file['error'],  __( 'Image Upload Error' ) ); 
    682  
    683         $url = $file['url']; 
    684         $type = $file['type']; 
    685         $file = $file['file']; 
    686         $filename = basename($file); 
    687  
    688         // Construct the object array 
    689         $object = array( 
    690         'post_title' => $filename, 
    691         'post_content' => $url, 
    692         'post_mime_type' => $type, 
    693         'guid' => $url, 
    694         'context' => 'custom-header' 
    695         ); 
    696  
    697         // Save the data 
    698         $id = wp_insert_attachment($object, $file); 
     694        if ( empty( $_POST ) && isset( $_GET['file'] ) ) { 
     695            $id = absint( $_GET['file'] ); 
     696            $file = get_attached_file( $id, true ); 
     697            $url = wp_get_attachment_image_src( $id, 'full'); 
     698            $url = $url[0]; 
     699        } elseif ( isset( $_POST ) ) { 
     700            extract($this->step_2_manage_upload()); 
     701        } 
    699702 
    700703        list($width, $height, $type, $attr) = getimagesize( $file ); 
     
    754757    <input type="hidden" name="attachment_id" id="attachment_id" value="<?php echo esc_attr( $id ); ?>" /> 
    755758    <input type="hidden" name="oitar" id="oitar" value="<?php echo esc_attr( $oitar ); ?>" /> 
     759    <?php if ( empty( $_POST ) && isset( $_GET['file'] ) ) { ?> 
     760    <input type="hidden" name="new-attachment" value="true" /> 
     761    <?php } ?> 
    756762    <?php wp_nonce_field( 'custom-header-crop-image' ) ?> 
    757763 
     
    761767</div> 
    762768        <?php 
     769    } 
     770 
     771 
     772    function step_2_manage_upload() { 
     773        $overrides = array('test_form' => false); 
     774        $file = wp_handle_upload($_FILES['import'], $overrides); 
     775 
     776        if ( isset($file['error']) ) 
     777            wp_die( $file['error'],  __( 'Image Upload Error' ) ); 
     778 
     779        $url = $file['url']; 
     780        $type = $file['type']; 
     781        $file = $file['file']; 
     782        $filename = basename($file); 
     783 
     784        // Construct the object array 
     785        $object = array( 
     786            'post_title'     => $filename, 
     787            'post_content'   => $url, 
     788            'post_mime_type' => $type, 
     789            'guid'           => $url, 
     790            'context'        => 'custom-header' 
     791        ); 
     792 
     793        // Save the data 
     794        $id = wp_insert_attachment( $object, $file ); 
     795        return compact( 'id', 'file', 'filename', 'url', 'type' ); 
    763796    } 
    764797 
     
    827860            'context' => 'custom-header' 
    828861        ); 
     862        if ( isset( $_POST['new-attachment'] ) && $_POST['new-attachment'] ) 
     863            unset($object['ID']); 
    829864 
    830865        // Update the attachment 
    831         wp_insert_attachment($object, $cropped); 
     866        $attachment_id = wp_insert_attachment( $object, $cropped ); 
    832867        wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) ); 
    833868        update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) ); 
     
    846881        // cleanup 
    847882        $medium = str_replace(basename($original), 'midsize-'.basename($original), $original); 
    848         @unlink( apply_filters( 'wp_delete_file', $medium ) ); 
    849         @unlink( apply_filters( 'wp_delete_file', $original ) ); 
     883        if ( file_exists( $medium ) ) 
     884            @unlink( apply_filters( 'wp_delete_file', $medium ) ); 
     885        if ( empty ( $_POST['new-attachment'] ) ) 
     886            @unlink( apply_filters( 'wp_delete_file', $original ) ); 
    850887 
    851888        return $this->finished(); 
     
    871908            wp_die(__('You do not have permission to customize headers.')); 
    872909        $step = $this->step(); 
    873         if ( 1 == $step || ! $_POST ) 
    874             $this->step_1(); 
    875         elseif ( 2 == $step ) 
     910        if ( 2 == $step ) 
    876911            $this->step_2(); 
    877912        elseif ( 3 == $step ) 
    878913            $this->step_3(); 
     914        else 
     915            $this->step_1(); 
     916    } 
     917 
     918    function attachment_fields_to_edit( $form_fields, $post ) { 
     919        if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' ) { 
     920            $form_fields = array(); 
     921            $href = esc_url(add_query_arg(array( 
     922                'page' => 'custom-header', 
     923                'step' => 2, 
     924                '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'), 
     925                'file' => $post->ID 
     926            ), admin_url('themes.php'))); 
     927 
     928            $form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td><a data-location="' . $href . '" class="wp-set-header">' . _( 'Set as header' ) . '</a></td></tr>' ); 
     929            $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-header' ); 
     930        } 
     931 
     932        return $form_fields; 
     933    } 
     934 
     935    function filter_upload_tabs( $tabs ) { 
     936        if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' ) 
     937            return array( 'library' => __('Media Library') ); 
     938 
     939        return $tabs; 
    879940    } 
    880941 
  • trunk/wp-admin/includes/media.php

    r20310 r20358  
    404404} 
    405405 
    406 function get_upload_iframe_src( $type = null, $post_id = null ) { 
     406function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) { 
    407407    global $post_ID; 
    408408 
     
    410410        $post_id = $post_ID; 
    411411 
    412     $uploading_iframe_ID = (int) $post_id; 
    413     $upload_iframe_src = add_query_arg( 'post_id', $uploading_iframe_ID, admin_url('media-upload.php') ); 
     412    $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') ); 
    414413 
    415414    if ( $type && 'media' != $type ) 
    416415        $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src); 
     416 
     417    if ( ! empty( $tab ) ) 
     418        $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src); 
    417419 
    418420    $upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src); 
     
    498500        $attachment = stripslashes_deep( $_POST['attachments'][$send_id] ); 
    499501 
    500         $html = $attachment['post_title']; 
     502        $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : ''; 
    501503        if ( !empty($attachment['url']) ) { 
    502504            $rel = ''; 
  • trunk/wp-admin/media-upload.php

    r20143 r20358  
    2323wp_enqueue_script('set-post-thumbnail' ); 
    2424wp_enqueue_style('imgareaselect'); 
     25wp_enqueue_script( 'media-gallery' ); 
    2526 
    2627@header('Content-Type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset')); 
  • trunk/wp-includes/script-loader.php

    r20340 r20358  
    418418 
    419419        $scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array('farbtastic'), false, 1 ); 
     420        $scripts->add( 'media-gallery', "/wp-admin/js/media-gallery$suffix.js", array('jquery'), false, 1 ); 
    420421    } 
    421422} 
Note: See TracChangeset for help on using the changeset viewer.