Attachment data abstraction from mdawaffe. fixes #3440

git-svn-id: http://svn.automattic.com/wordpress/trunk@4612 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2006-12-05 22:37:19 +00:00
parent 8689df80a5
commit 995d839bfb
8 changed files with 61 additions and 28 deletions

View File

@ -2016,7 +2016,7 @@ function the_attachment_links( $id = false ) {
return false;
$icon = get_attachment_icon( $post->ID );
$attachment_data = get_post_meta( $id, '_wp_attachment_metadata', true );
$attachment_data = wp_get_attachment_metadata( $id );
$thumb = isset( $attachment_data['thumb'] );
?>
<form id="the-attachment-links">

View File

@ -71,13 +71,10 @@ case 'editattachment':
$_POST['post_type'] = 'attachment';
// Update the thumbnail filename
$oldmeta = $newmeta = get_post_meta($page_id, '_wp_attachment_metadata', true);
$newmeta = wp_get_attachment_metadata( $page_id, true );
$newmeta['thumb'] = $_POST['thumb'];
if ( '' !== $oldmeta )
update_post_meta($page_id, '_wp_attachment_metadata', $newmeta, $oldmeta);
else
add_post_meta($page_id, '_wp_attachment_metadata', $newmeta);
wp_update_attachment_metadata( $newmeta );
case 'editpost':
$page_ID = (int) $_POST['post_ID'];

View File

@ -78,13 +78,10 @@ case 'editattachment':
$_POST['post_type'] = 'attachment';
// Update the thumbnail filename
$oldmeta = $newmeta = get_post_meta($post_id, '_wp_attachment_metadata', true);
$newmeta = wp_get_attachment_metadata( $post_id, true );
$newmeta['thumb'] = $_POST['thumb'];
if ( '' !== $oldmeta )
update_post_meta($post_id, '_wp_attachment_metadata', $newmeta, $oldmeta);
else
add_post_meta($post_id, '_wp_attachment_metadata', $newmeta);
wp_update_attachment_metadata( $post_id, $newmeta );
case 'editpost':
$post_ID = (int) $_POST['post_ID'];

View File

@ -466,7 +466,7 @@ function upgrade_160() {
$meta = get_post_meta($object->ID, 'imagedata', true);
if ( ! empty($meta['file']) )
add_post_meta($object->ID, '_wp_attached_file', $meta['file']);
update_attached_file( $object->ID, $meta['file'] );
}
}
}

View File

@ -2,7 +2,7 @@
function wp_upload_display( $dims = false, $href = '' ) {
global $post;
$id = get_the_ID();
$attachment_data = get_post_meta( $id, '_wp_attachment_metadata', true );
$attachment_data = wp_get_attachment_metadata( $id );
if ( isset($attachment_data['width']) )
list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);
ob_start();
@ -57,7 +57,7 @@ function wp_upload_display( $dims = false, $href = '' ) {
function wp_upload_view() {
global $style, $post_id, $style;
$id = get_the_ID();
$attachment_data = get_post_meta( $id, '_wp_attachment_metadata', true );
$attachment_data = wp_get_attachment_metadata( $id );
?>
<div id="upload-file">
<div id="file-title">
@ -98,7 +98,7 @@ function wp_upload_form() {
<?php
if ( $id ) :
$attachment = get_post_to_edit( $id );
$attachment_data = get_post_meta( $id, '_wp_attachment_metadata', true );
$attachment_data = wp_get_attachment_metadata( $id );
?>
<div id="file-title">
<h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style )
@ -229,7 +229,7 @@ function wp_upload_tab_upload_action() {
$imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'";
$imagedata['file'] = $file;
add_post_meta($id, '_wp_attachment_metadata', $imagedata);
wp_update_attachment_metadata( $id, $imagedata );
if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
@ -240,13 +240,13 @@ function wp_upload_tab_upload_action() {
if ( @file_exists($thumb) ) {
$newdata = $imagedata;
$newdata['thumb'] = basename($thumb);
update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
wp_update_attachment_metadata( $id, $newdata );
} else {
$error = $thumb;
}
}
} else {
add_post_meta($id, '_wp_attachment_metadata', array());
wp_update_attachment_metadata( $id, array() );
}
wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=browse&action=view&ID=$id&post_id=$post_id");

View File

@ -353,12 +353,11 @@ function get_attachment_icon($id = 0, $fullsize = false, $max_dims = false) {
$mime = $post->post_mime_type;
$imagedata = get_post_meta($post->ID, '_wp_attachment_metadata', true);
$imagedata = wp_get_attachment_metadata( $post->ID );
$file = get_post_meta($post->ID, '_wp_attached_file', true);
$file = get_attached_file( $post->ID );
$exts = array('jpg', 'gif', 'png');
if ( !$fullsize && !empty($imagedata['thumb'])
&& ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file))
&& file_exists($thumbfile) ) {

View File

@ -4,8 +4,25 @@
// Post functions
//
function get_attached_file($attachment_id) {
return get_post_meta($attachment_id, '_wp_attached_file', true);
function get_attached_file( $attachment_id, $unfiltered = false ) {
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
if ( $unfiltered )
return $file;
return apply_filters( 'get_attached_file', $file, $attachment_id );
}
function update_attached_file( $attachment_id, $file ) {
if ( !get_post( $attachment_id ) )
return false;
$old_file = get_attached_file( $attachment_id, true );
$file = apply_filters( 'update_attached_file', $file, $attachment_id );
if ( $old_file )
return update_post_meta( $attachment_id, '_wp_attached_file', $file, $old_file );
else
return add_post_meta( $attachment_id, '_wp_attached_file', $file );
}
function &get_children($args = '', $output = OBJECT) {
@ -1331,7 +1348,7 @@ function wp_insert_attachment($object, $file = false, $post_parent = 0) {
wp_set_post_categories($post_ID, $post_category);
if ( $file )
add_post_meta($post_ID, '_wp_attached_file', $file);
update_attached_file( $post_ID, $file );
clean_post_cache($post_ID);
@ -1354,8 +1371,8 @@ function wp_delete_attachment($postid) {
if ( 'attachment' != $post->post_type )
return false;
$meta = get_post_meta($postid, '_wp_attachment_metadata', true);
$file = get_post_meta($postid, '_wp_attached_file', true);
$meta = wp_get_attachment_metadata( $postid );
$file = get_attached_file( $postid );
$wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'");
@ -1384,4 +1401,27 @@ function wp_delete_attachment($postid) {
return $post;
}
function wp_get_attachment_metadata( $post_id, $unfiltered = false ) {
$post_id = (int) $post_id;
$data = get_post_meta( $post_id, '_wp_attachment_metadata', true );
if ( $unfiltered )
return $data;
return apply_filters( 'wp_get_attachment_metadata', $data, $post_id );
}
function wp_update_attachment_metadata( $post_id, $data ) {
if ( !get_post( $post_id ) )
return false;
$old_data = wp_get_attachment_metadata( $post_id, true );
$data = apply_filters( 'wp_update_attachment_metadata', $data, $post_id );
if ( $old_data )
return update_post_meta( $post_id, '_wp_attachment_metadata', $data, $old_data );
else
return add_post_meta( $post_id, '_wp_attachment_metadata', $data );
}
?>

View File

@ -871,8 +871,8 @@ class wp_xmlrpc_server extends IXR_Server {
'guid' => $upload[ 'url' ]
);
// Save the data
$id = wp_insert_attachment($attachment, $upload[ 'file' ], $post_id);
add_post_meta($id, '_wp_attachment_metadata', array());
$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
wp_update_attachment_metadata( $id, array() );
return apply_filters( 'wp_handle_upload', array( 'file' => $upload[ 'file' ], 'url' => $upload[ 'url' ], 'type' => $type ) );
}