Make WordPress Core


Ignore:
Timestamp:
04/05/2012 12:20:28 AM (15 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.