From d3134e2688db9405d2203c6e493abecd63ca93e9 Mon Sep 17 00:00:00 2001
From: ryan
Date: Thu, 5 Apr 2012 00:20:28 +0000
Subject: [PATCH] Allow selecting custom header and background images from the
media library. Props aaroncampbell, sabreuse, greuben. fixes #19840
git-svn-id: http://svn.automattic.com/wordpress/trunk@20358 1a063a9b-81f0-0310-95a4-ce76da25c4cd
---
wp-admin/custom-background.php | 46 +++++++++++-
wp-admin/custom-header.php | 129 ++++++++++++++++++++++++---------
wp-admin/includes/media.php | 10 ++-
wp-admin/media-upload.php | 1 +
wp-includes/script-loader.php | 1 +
5 files changed, 148 insertions(+), 39 deletions(-)
diff --git a/wp-admin/custom-background.php b/wp-admin/custom-background.php
index a5b5f9dc76..c4517d4176 100644
--- a/wp-admin/custom-background.php
+++ b/wp-admin/custom-background.php
@@ -55,6 +55,7 @@ class Custom_Background {
$this->admin_image_div_callback = $admin_image_div_callback;
add_action( 'admin_menu', array( $this, 'init' ) );
+ add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
}
/**
@@ -71,6 +72,8 @@ class Custom_Background {
add_action("load-$page", array(&$this, 'admin_load'));
add_action("load-$page", array(&$this, 'take_action'), 49);
add_action("load-$page", array(&$this, 'handle_upload'), 49);
+ add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
+ add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
if ( $this->admin_header_callback )
add_action("admin_head-$page", $this->admin_header_callback, 51);
@@ -98,6 +101,8 @@ class Custom_Background {
'' . __( 'Support Forums' ) . '
'
);
+ add_thickbox();
+ wp_enqueue_script('media-upload');
wp_enqueue_script('custom-background');
wp_enqueue_style('farbtastic');
}
@@ -126,6 +131,7 @@ class Custom_Background {
set_theme_mod('background_image', '');
set_theme_mod('background_image_thumb', '');
$this->updated = true;
+ wp_safe_redirect( $_POST['_wp_http_referer'] );
return;
}
@@ -248,7 +254,13 @@ if ( get_background_image() ) {
-
+ 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
+?>
+
+
@@ -368,4 +380,36 @@ if ( get_background_image() ) {
$this->updated = true;
}
+ function attachment_fields_to_edit( $form_fields, $post ) {
+ if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) {
+ $form_fields = array( 'image-size' => $form_fields['image-size'] );
+ $form_fields['buttons'] = array( 'tr' => ' | ' . _( 'Set as background' ) . ' |
' );
+ $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
+ }
+
+ return $form_fields;
+ }
+
+ function filter_upload_tabs ( $tabs ){
+ if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' )
+ return array( 'library' => __('Media Library') );
+
+ return $tabs;
+ }
+
+ public function wp_set_background_image() {
+ if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
+ $attachment_id = absint($_POST['attachment_id']);
+ $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
+ $size = 'thumbnail';
+ if ( in_array( $_POST['size'], $sizes ) )
+ $size = esc_attr( $_POST['size'] );
+
+ update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
+ $url = wp_get_attachment_image_src( $attachment_id, $size );
+ $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
+ set_theme_mod( 'background_image', esc_url( $url[0] ) );
+ set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) );
+ exit;
+ }
}
diff --git a/wp-admin/custom-header.php b/wp-admin/custom-header.php
index 841d61f985..98030012ca 100644
--- a/wp-admin/custom-header.php
+++ b/wp-admin/custom-header.php
@@ -92,6 +92,9 @@ class Custom_Image_Header {
add_action("admin_head-$page", array(&$this, 'take_action'), 50);
add_action("admin_head-$page", array(&$this, 'js'), 50);
add_action("admin_head-$page", $this->admin_header_callback, 51);
+
+ add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
+ add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
}
/**
@@ -128,8 +131,11 @@ class Custom_Image_Header {
return 1;
$step = (int) $_GET['step'];
- if ( $step < 1 || 3 < $step )
- $step = 1;
+ if ( $step < 1 || 3 < $step ||
+ ( 2 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce-custom-header-upload'], 'custom-header-upload' ) ) ||
+ ( 3 == $step && ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'custom-header-crop-image' ) )
+ )
+ return 1;
return $step;
}
@@ -142,10 +148,15 @@ class Custom_Image_Header {
function js_includes() {
$step = $this->step();
- if ( ( 1 == $step || 3 == $step ) && current_theme_supports( 'custom-header', 'header-text' ) )
- wp_enqueue_script('farbtastic');
- elseif ( 2 == $step )
+ if ( ( 1 == $step || 3 == $step ) ) {
+ add_thickbox();
+ wp_enqueue_script( 'media-upload' );
+ wp_enqueue_script( 'custom-header' );
+ if ( current_theme_supports( 'custom-header', 'header-text' ) )
+ wp_enqueue_script('farbtastic');
+ } elseif ( 2 == $step ) {
wp_enqueue_script('imgareaselect');
+ }
}
/**
@@ -395,7 +406,7 @@ var farbtastic;
pickColor('#');
- toggle_text();
+ toggle_text();
});
})(jQuery);
@@ -553,6 +564,12 @@ var farbtastic;
+ 'custom-header', 'TB_iframe' => 1 ), $image_library_url );
+ ?>
+
@@ -674,28 +691,14 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
if ( ! current_theme_supports( 'custom-header', 'uploads' ) )
wp_die( __( 'Cheatin’ uh?' ) );
- $overrides = array('test_form' => false);
- $file = wp_handle_upload($_FILES['import'], $overrides);
-
- if ( isset($file['error']) )
- wp_die( $file['error'], __( 'Image Upload Error' ) );
-
- $url = $file['url'];
- $type = $file['type'];
- $file = $file['file'];
- $filename = basename($file);
-
- // Construct the object array
- $object = array(
- 'post_title' => $filename,
- 'post_content' => $url,
- 'post_mime_type' => $type,
- 'guid' => $url,
- 'context' => 'custom-header'
- );
-
- // Save the data
- $id = wp_insert_attachment($object, $file);
+ if ( empty( $_POST ) && isset( $_GET['file'] ) ) {
+ $id = absint( $_GET['file'] );
+ $file = get_attached_file( $id, true );
+ $url = wp_get_attachment_image_src( $id, 'full');
+ $url = $url[0];
+ } elseif ( isset( $_POST ) ) {
+ extract($this->step_2_manage_upload());
+ }
list($width, $height, $type, $attr) = getimagesize( $file );
@@ -753,6 +756,9 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
+
+
+
@@ -762,6 +768,33 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
false);
+ $file = wp_handle_upload($_FILES['import'], $overrides);
+
+ if ( isset($file['error']) )
+ wp_die( $file['error'], __( 'Image Upload Error' ) );
+
+ $url = $file['url'];
+ $type = $file['type'];
+ $file = $file['file'];
+ $filename = basename($file);
+
+ // Construct the object array
+ $object = array(
+ 'post_title' => $filename,
+ 'post_content' => $url,
+ 'post_mime_type' => $type,
+ 'guid' => $url,
+ 'context' => 'custom-header'
+ );
+
+ // Save the data
+ $id = wp_insert_attachment( $object, $file );
+ return compact( 'id', 'file', 'filename', 'url', 'type' );
+ }
+
/**
* Display third step of custom header image page.
*
@@ -826,9 +859,11 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
'guid' => $url,
'context' => 'custom-header'
);
+ if ( isset( $_POST['new-attachment'] ) && $_POST['new-attachment'] )
+ unset($object['ID']);
// Update the attachment
- wp_insert_attachment($object, $cropped);
+ $attachment_id = wp_insert_attachment( $object, $cropped );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $cropped ) );
update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', get_option('stylesheet' ) );
@@ -845,8 +880,10 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
// cleanup
$medium = str_replace(basename($original), 'midsize-'.basename($original), $original);
- @unlink( apply_filters( 'wp_delete_file', $medium ) );
- @unlink( apply_filters( 'wp_delete_file', $original ) );
+ if ( file_exists( $medium ) )
+ @unlink( apply_filters( 'wp_delete_file', $medium ) );
+ if ( empty ( $_POST['new-attachment'] ) )
+ @unlink( apply_filters( 'wp_delete_file', $original ) );
return $this->finished();
}
@@ -870,12 +907,36 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?>
if ( ! current_user_can('edit_theme_options') )
wp_die(__('You do not have permission to customize headers.'));
$step = $this->step();
- if ( 1 == $step || ! $_POST )
- $this->step_1();
- elseif ( 2 == $step )
+ if ( 2 == $step )
$this->step_2();
elseif ( 3 == $step )
$this->step_3();
+ else
+ $this->step_1();
+ }
+
+ function attachment_fields_to_edit( $form_fields, $post ) {
+ if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' ) {
+ $form_fields = array();
+ $href = esc_url(add_query_arg(array(
+ 'page' => 'custom-header',
+ 'step' => 2,
+ '_wpnonce-custom-header-upload' => wp_create_nonce('custom-header-upload'),
+ 'file' => $post->ID
+ ), admin_url('themes.php')));
+
+ $form_fields['buttons'] = array( 'tr' => ' | |
' );
+ $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-header' );
+ }
+
+ return $form_fields;
+ }
+
+ function filter_upload_tabs( $tabs ) {
+ if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-header' )
+ return array( 'library' => __('Media Library') );
+
+ return $tabs;
}
}
diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php
index ced9e52700..9be8e23924 100644
--- a/wp-admin/includes/media.php
+++ b/wp-admin/includes/media.php
@@ -403,18 +403,20 @@ function _media_button($title, $icon, $type, $id) {
return "";
}
-function get_upload_iframe_src( $type = null, $post_id = null ) {
+function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
global $post_ID;
if ( empty( $post_id ) )
$post_id = $post_ID;
- $uploading_iframe_ID = (int) $post_id;
- $upload_iframe_src = add_query_arg( 'post_id', $uploading_iframe_ID, admin_url('media-upload.php') );
+ $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
if ( $type && 'media' != $type )
$upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
+ if ( ! empty( $tab ) )
+ $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
+
$upload_iframe_src = apply_filters($type . '_upload_iframe_src', $upload_iframe_src);
return add_query_arg('TB_iframe', true, $upload_iframe_src);
@@ -497,7 +499,7 @@ function media_upload_form_handler() {
if ( isset($send_id) ) {
$attachment = stripslashes_deep( $_POST['attachments'][$send_id] );
- $html = $attachment['post_title'];
+ $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
if ( !empty($attachment['url']) ) {
$rel = '';
if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
diff --git a/wp-admin/media-upload.php b/wp-admin/media-upload.php
index 394ba04082..e8e50222dc 100644
--- a/wp-admin/media-upload.php
+++ b/wp-admin/media-upload.php
@@ -22,6 +22,7 @@ wp_enqueue_script('plupload-handlers');
wp_enqueue_script('image-edit');
wp_enqueue_script('set-post-thumbnail' );
wp_enqueue_style('imgareaselect');
+wp_enqueue_script( 'media-gallery' );
@header('Content-Type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php
index d8371cb4b7..89dd0875cc 100644
--- a/wp-includes/script-loader.php
+++ b/wp-includes/script-loader.php
@@ -417,6 +417,7 @@ function wp_default_scripts( &$scripts ) {
) );
$scripts->add( 'custom-background', "/wp-admin/js/custom-background$suffix.js", array('farbtastic'), false, 1 );
+ $scripts->add( 'media-gallery', "/wp-admin/js/media-gallery$suffix.js", array('jquery'), false, 1 );
}
}