Post thumbnail removal functionality (only allowed for replacement before). props scribu. see #10928

git-svn-id: http://svn.automattic.com/wordpress/trunk@12036 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
markjaquith 2009-10-15 12:57:16 +00:00
parent 0dc4500e09
commit af407e7e3f
7 changed files with 38 additions and 6 deletions

View File

@ -1395,6 +1395,12 @@ case 'set-post-thumbnail':
if ( !current_user_can( 'edit_post', $post_id ) )
die( '-1' );
$thumbnail_id = intval( $_POST['thumbnail_id'] );
if ( $thumbnail_id == '-1' ) {
delete_post_meta( $post_id, '_thumbnail_id' );
die( _wp_post_thumbnail_html() );
}
if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
if ( !empty( $thumbnail_html ) ) {

View File

@ -115,12 +115,17 @@ function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = fal
}
function _wp_post_thumbnail_html( $thumbnail_id = NULL ) {
$content = '<p class="hide-if-no-js"><a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . esc_html__( 'Set thumbnail' ) . '</a></p>';
if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
$thumbnail_html = wp_get_attachment_image($thumbnail_id, array( 266, 266 ) );
if ( !empty( $thumbnail_html ) )
return apply_filters( 'admin_post_thumbnail_html', '<a href="#" id="cws-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . $thumbnail_html . '</a>' );
if ( !empty( $thumbnail_html ) ) {
$content = '<a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . $thumbnail_html . '</a>';
$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail();return false;">' . esc_html__( 'Remove thumbnail' ) . '</a></p>';
}
}
return apply_filters( 'admin_post_thumbnail_html', '<a href="#" id="cws-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . esc_html__( 'Set thumbnail' ) . '</a>' );
return apply_filters( 'admin_post_thumbnail_html', $content );
}
function post_thumbnail_meta_box() {

View File

@ -1,8 +1,22 @@
function WPSetThumbnailHTML(html){
jQuery('#postthumbnaildiv .inside').html(html);
}
function WPSetThumbnailID(id){
if ( jQuery('input[value=_thumbnail_id]').size() > 0 ) {
jQuery('#meta\\[' + jQuery('input[value=_thumbnail_id]').attr('id').match(/[0-9]+/) + '\\]\\[value\\]' ).text(id);
}
}
}
function WPRemoveThumbnail(){
jQuery.post(ajaxurl, {
action:"set-post-thumbnail", post_id: jQuery('#post_ID').val(), thumbnail_id: -1, cookie: encodeURIComponent(document.cookie)
}, function(str){
if ( str == '0' ) {
alert( setPostThumbnailL10n.error );
} else {
WPSetThumbnailHTML(str);
}
}
);
}

View File

@ -1 +1 @@
function WPSetThumbnailHTML(html){jQuery("#postthumbnaildiv .inside").html(html)}function WPSetThumbnailID(id){if(jQuery("input[value=_thumbnail_id]").size()>0){jQuery("#meta\\["+jQuery("input[value=_thumbnail_id]").attr("id").match(/[0-9]+/)+"\\]\\[value\\]").text(id)}};
function WPSetThumbnailHTML(html){jQuery("#postthumbnaildiv .inside").html(html)}function WPSetThumbnailID(id){if(jQuery("input[value=_thumbnail_id]").size()>0){jQuery("#meta\\["+jQuery("input[value=_thumbnail_id]").attr("id").match(/[0-9]+/)+"\\]\\[value\\]").text(id)}}function WPRemoveThumbnail(){jQuery.post(ajaxurl,{action:"set-post-thumbnail",post_id:jQuery("#post_ID").val(),thumbnail_id:-1,cookie:encodeURIComponent(document.cookie)},function(str){if(str=="0"){alert(setPostThumbnailL10n.error)}else{WPSetThumbnailHTML(str)}})};

View File

@ -17,6 +17,9 @@ get_header(); ?>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php if ( has_post_image() ) : ?>
<p class="post-thumbnail"><?php the_post_image( array( '150', '150') ) ?></p>
<?php endif; ?>
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

View File

@ -712,6 +712,10 @@ div.aligncenter {
}
/* End captions */
p.post-thumbnail {
float: left;
margin: 0 10px 10px 0;
}
/* "Daisy, Daisy, give me your answer do. I'm half crazy all for the love of you.
It won't be a stylish marriage, I can't afford a carriage.

View File

@ -383,7 +383,7 @@ function wp_default_scripts( &$scripts ) {
'error' => __( 'Could not set that as the thumbnail image. Try a different attachment.' )
) );
$scripts->add( 'set-post-thumbnail-handler', "/wp-admin/js/set-post-thumbnail-handler$suffix.js", array( 'jquery' ), '20091014' );
$scripts->add( 'set-post-thumbnail-handler', "/wp-admin/js/set-post-thumbnail-handler$suffix.js", array( 'jquery' ), '20091015' );
$scripts->add_data( 'set-post-thumbnail-handler', 'group', 1 );
}