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:
parent
8689df80a5
commit
995d839bfb
|
@ -2016,7 +2016,7 @@ function the_attachment_links( $id = false ) {
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
$icon = get_attachment_icon( $post->ID );
|
$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'] );
|
$thumb = isset( $attachment_data['thumb'] );
|
||||||
?>
|
?>
|
||||||
<form id="the-attachment-links">
|
<form id="the-attachment-links">
|
||||||
|
|
|
@ -71,13 +71,10 @@ case 'editattachment':
|
||||||
$_POST['post_type'] = 'attachment';
|
$_POST['post_type'] = 'attachment';
|
||||||
|
|
||||||
// Update the thumbnail filename
|
// 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'];
|
$newmeta['thumb'] = $_POST['thumb'];
|
||||||
|
|
||||||
if ( '' !== $oldmeta )
|
wp_update_attachment_metadata( $newmeta );
|
||||||
update_post_meta($page_id, '_wp_attachment_metadata', $newmeta, $oldmeta);
|
|
||||||
else
|
|
||||||
add_post_meta($page_id, '_wp_attachment_metadata', $newmeta);
|
|
||||||
|
|
||||||
case 'editpost':
|
case 'editpost':
|
||||||
$page_ID = (int) $_POST['post_ID'];
|
$page_ID = (int) $_POST['post_ID'];
|
||||||
|
|
|
@ -78,13 +78,10 @@ case 'editattachment':
|
||||||
$_POST['post_type'] = 'attachment';
|
$_POST['post_type'] = 'attachment';
|
||||||
|
|
||||||
// Update the thumbnail filename
|
// 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'];
|
$newmeta['thumb'] = $_POST['thumb'];
|
||||||
|
|
||||||
if ( '' !== $oldmeta )
|
wp_update_attachment_metadata( $post_id, $newmeta );
|
||||||
update_post_meta($post_id, '_wp_attachment_metadata', $newmeta, $oldmeta);
|
|
||||||
else
|
|
||||||
add_post_meta($post_id, '_wp_attachment_metadata', $newmeta);
|
|
||||||
|
|
||||||
case 'editpost':
|
case 'editpost':
|
||||||
$post_ID = (int) $_POST['post_ID'];
|
$post_ID = (int) $_POST['post_ID'];
|
||||||
|
|
|
@ -466,7 +466,7 @@ function upgrade_160() {
|
||||||
|
|
||||||
$meta = get_post_meta($object->ID, 'imagedata', true);
|
$meta = get_post_meta($object->ID, 'imagedata', true);
|
||||||
if ( ! empty($meta['file']) )
|
if ( ! empty($meta['file']) )
|
||||||
add_post_meta($object->ID, '_wp_attached_file', $meta['file']);
|
update_attached_file( $object->ID, $meta['file'] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
function wp_upload_display( $dims = false, $href = '' ) {
|
function wp_upload_display( $dims = false, $href = '' ) {
|
||||||
global $post;
|
global $post;
|
||||||
$id = get_the_ID();
|
$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']) )
|
if ( isset($attachment_data['width']) )
|
||||||
list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);
|
list($width,$height) = wp_shrink_dimensions($attachment_data['width'], $attachment_data['height'], 171, 128);
|
||||||
ob_start();
|
ob_start();
|
||||||
|
@ -57,7 +57,7 @@ function wp_upload_display( $dims = false, $href = '' ) {
|
||||||
function wp_upload_view() {
|
function wp_upload_view() {
|
||||||
global $style, $post_id, $style;
|
global $style, $post_id, $style;
|
||||||
$id = get_the_ID();
|
$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="upload-file">
|
||||||
<div id="file-title">
|
<div id="file-title">
|
||||||
|
@ -98,7 +98,7 @@ function wp_upload_form() {
|
||||||
<?php
|
<?php
|
||||||
if ( $id ) :
|
if ( $id ) :
|
||||||
$attachment = get_post_to_edit( $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">
|
<div id="file-title">
|
||||||
<h2><?php if ( !isset($attachment_data['width']) && 'inline' != $style )
|
<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['hwstring_small'] = "height='$uheight' width='$uwidth'";
|
||||||
$imagedata['file'] = $file;
|
$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'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
|
||||||
if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
|
if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
|
||||||
|
@ -240,13 +240,13 @@ function wp_upload_tab_upload_action() {
|
||||||
if ( @file_exists($thumb) ) {
|
if ( @file_exists($thumb) ) {
|
||||||
$newdata = $imagedata;
|
$newdata = $imagedata;
|
||||||
$newdata['thumb'] = basename($thumb);
|
$newdata['thumb'] = basename($thumb);
|
||||||
update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
|
wp_update_attachment_metadata( $id, $newdata );
|
||||||
} else {
|
} else {
|
||||||
$error = $thumb;
|
$error = $thumb;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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");
|
wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=browse&action=view&ID=$id&post_id=$post_id");
|
||||||
|
|
|
@ -353,12 +353,11 @@ function get_attachment_icon($id = 0, $fullsize = false, $max_dims = false) {
|
||||||
|
|
||||||
$mime = $post->post_mime_type;
|
$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');
|
$exts = array('jpg', 'gif', 'png');
|
||||||
|
|
||||||
if ( !$fullsize && !empty($imagedata['thumb'])
|
if ( !$fullsize && !empty($imagedata['thumb'])
|
||||||
&& ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file))
|
&& ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file))
|
||||||
&& file_exists($thumbfile) ) {
|
&& file_exists($thumbfile) ) {
|
||||||
|
|
|
@ -4,8 +4,25 @@
|
||||||
// Post functions
|
// Post functions
|
||||||
//
|
//
|
||||||
|
|
||||||
function get_attached_file($attachment_id) {
|
function get_attached_file( $attachment_id, $unfiltered = false ) {
|
||||||
return get_post_meta($attachment_id, '_wp_attached_file', true);
|
$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) {
|
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);
|
wp_set_post_categories($post_ID, $post_category);
|
||||||
|
|
||||||
if ( $file )
|
if ( $file )
|
||||||
add_post_meta($post_ID, '_wp_attached_file', $file);
|
update_attached_file( $post_ID, $file );
|
||||||
|
|
||||||
clean_post_cache($post_ID);
|
clean_post_cache($post_ID);
|
||||||
|
|
||||||
|
@ -1354,8 +1371,8 @@ function wp_delete_attachment($postid) {
|
||||||
if ( 'attachment' != $post->post_type )
|
if ( 'attachment' != $post->post_type )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
$meta = get_post_meta($postid, '_wp_attachment_metadata', true);
|
$meta = wp_get_attachment_metadata( $postid );
|
||||||
$file = get_post_meta($postid, '_wp_attached_file', true);
|
$file = get_attached_file( $postid );
|
||||||
|
|
||||||
$wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'");
|
$wpdb->query("DELETE FROM $wpdb->posts WHERE ID = '$postid'");
|
||||||
|
|
||||||
|
@ -1384,4 +1401,27 @@ function wp_delete_attachment($postid) {
|
||||||
return $post;
|
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 );
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -872,7 +872,7 @@ class wp_xmlrpc_server extends IXR_Server {
|
||||||
);
|
);
|
||||||
// Save the data
|
// Save the data
|
||||||
$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
|
$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id );
|
||||||
add_post_meta($id, '_wp_attachment_metadata', array());
|
wp_update_attachment_metadata( $id, array() );
|
||||||
|
|
||||||
return apply_filters( 'wp_handle_upload', array( 'file' => $upload[ 'file' ], 'url' => $upload[ 'url' ], 'type' => $type ) );
|
return apply_filters( 'wp_handle_upload', array( 'file' => $upload[ 'file' ], 'url' => $upload[ 'url' ], 'type' => $type ) );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue