Attachment cleanups from skeltoac. fixes #1870

git-svn-id: https://develop.svn.wordpress.org/trunk@3145 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2005-11-18 09:25:47 +00:00
parent f8d5219af6
commit 7a90a61c9d
4 changed files with 77 additions and 23 deletions

View File

@ -63,6 +63,9 @@ function write_post() {
if ( $_POST['temp_ID'] )
relocate_children($_POST['temp_ID'], $post_ID);
// Now that we have an ID we can fix any attachment anchor hrefs
fix_attachment_links($post_ID);
return $post_ID;
}
@ -74,6 +77,40 @@ function relocate_children($old_ID, $new_ID) {
return $wpdb->query("UPDATE $wpdb->posts SET post_parent = $new_ID WHERE post_parent = $old_ID");
}
// Replace hrefs of attachment anchors with up-to-date permalinks.
function fix_attachment_links($post_ID) {
global $wp_rewrite;
// Relevance check.
if ( false == $wp_rewrite->using_permalinks() )
return;
$post = & get_post($post_ID);
$search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie";
// See if we have any rel="attachment" links
if ( 0 == preg_match_all($search, $post->post_content, $anchor_matches, PREG_PATTERN_ORDER) )
return;
$i = 0;
$search = "# id=(\"|)(\d+)\\1#i";
foreach ( $anchor_matches[0] as $anchor ) {
echo "$search\n$anchor\n";
if ( 0 == preg_match($search, $anchor, $id_matches) )
continue;
$id = $id_matches[2];
$post_search[$i] = $anchor;
$post_replace[$i] = preg_replace("#href=(\"|')[^'\"]*\\1#e", "stripslashes('href=\\1').get_attachment_link($id).stripslashes('\\1')", $anchor);
++$i;
}
$post->post_content = str_replace($post_search, $post_replace, $post->post_content);
return wp_update_post($post);
}
// Update an existing post with values provided in $_POST.
function edit_post() {
global $user_ID;
@ -140,6 +177,9 @@ function edit_post() {
wp_update_post($_POST);
// Now that we have an ID we can fix any attachment anchor hrefs
fix_attachment_links($_POST['ID']);
// Meta Stuff
if ($_POST['meta'])
: foreach ($_POST['meta'] as $key => $value)
@ -1795,4 +1835,4 @@ function wp_import_handle_upload() {
return array('file' => $file, 'id' => $id);
}
?>
?>

View File

@ -105,7 +105,7 @@ tinyMCE.init({
convert_newlines_to_brs : false,
remove_linebreaks : true,
save_callback : "wp_save_callback",
valid_elements : "-a[href|title|rel],-strong/b,-em/i,-strike,-del,-u,p[class|align],-ol,-ul,-li,br,img[class|src|alt|title|width|height|align],-sub,-sup,-blockquote,-table[border=0|cellspacing|cellpadding|width|height|class|align],tr[class|rowspan|width|height|align|valign],td[dir|class|colspan|rowspan|width|height|align|valign],-div[dir|class|align],-span[class|align],-pre[class],address,-h1[class|align],-h2[class|align],-h3[class|align],-h4[class|align],-h5[class|align],-h6[class|align],hr",
valid_elements : "-a[id|href|title|rel],-strong/b,-em/i,-strike,-del,-u,p[class|align],-ol,-ul,-li,br,img[class|src|alt|title|width|height|align],-sub,-sup,-blockquote,-table[border=0|cellspacing|cellpadding|width|height|class|align],tr[class|rowspan|width|height|align|valign],td[dir|class|colspan|rowspan|width|height|align|valign],-div[dir|class|align],-span[class|align],-pre[class],address,-h1[class|align],-h2[class|align],-h3[class|align],-h4[class|align],-h5[class|align],-h6[class|align],hr",
plugins : "wordpress,autosave"
<?php do_action('mce_options'); ?>
});

View File

@ -77,7 +77,7 @@ $imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'";
$imagedata['file'] = $file;
$imagedata['thumb'] = "thumb-$filename";
add_post_meta($id, 'imagedata', $imagedata);
add_post_meta($id, '_wp_attachment_metadata', $imagedata);
if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 )
@ -135,7 +135,6 @@ if ( $start > 0 ) {
$back = false;
}
$i = 0;
$uwidth_sum = 0;
$images_html = '';
$images_style = '';
@ -154,15 +153,20 @@ if ( count($images) > 0 ) {
$images_script .= "attachmenton = '$__attachment_on';\nattachmentoff = '$__attachment_off';\n";
$images_script .= "thumbnailon = '$__thumbnail_on';\nthumbnailoff = '$__thumbnail_off';\n";
foreach ( $images as $key => $image ) {
$meta = get_post_meta($image['ID'], 'imagedata', true);
$attachment_ID = $image['ID'];
$meta = get_post_meta($attachment_ID, '_wp_attachment_metadata', true);
if (!is_array($meta)) {
wp_delete_attachment($image['ID']);
continue;
$meta = get_post_meta($attachment_ID, 'imagedata', true); // Try 1.6 Alpha meta key
if (!is_array($meta)) {
continue;
} else {
add_post_meta($attachment_ID, '_wp_attachment_metadata', $meta);
}
}
$image = array_merge($image, $meta);
if ( ($image['width'] > 128 || $image['height'] > 96) && !empty($image['thumb']) && file_exists(dirname($image['file']).'/'.$image['thumb']) ) {
$src = str_replace(basename($image['guid']), '', $image['guid']) . $image['thumb'];
$images_script .= "src".$i."a = '$src';\nsrc".$i."b = '".$image['guid']."';\n";
$images_script .= "src".$attachment_ID."a = '$src';\nsrc".$attachment_ID."b = '".$image['guid']."';\n";
$thumb = 'true';
$thumbtext = $__thumbnail_on;
} else {
@ -175,24 +179,22 @@ if ( count($images) > 0 ) {
$uwidth_sum += 128;
$xpadding = (128 - $image['uwidth']) / 2;
$ypadding = (96 - $image['uheight']) / 2;
$attachment = $image['ID'];
$images_style .= "#target$i img { padding: {$ypadding}px {$xpadding}px; }\n";
$href = get_attachment_link($attachment);
$images_script .= "href".$i."a = '$href';\nhref".$i."b = '{$image['guid']}';\n";
$images_style .= "#target{$attachment_ID} img { padding: {$ypadding}px {$xpadding}px; }\n";
$href = get_attachment_link($attachment_ID);
$images_script .= "href{$attachment_ID}a = '$href';\nhref{$attachment_ID}b = '{$image['guid']}';\n";
$images_html .= "
<div id='target$i' class='imagewrap left'>
<div id='popup$i' class='popup'>
<a id=\"L$i\" onclick=\"toggleLink($i);return false;\" href=\"javascript:void();\">$__attachment_on</a>
<a id=\"I$i\" onclick=\"if($thumb)toggleImage($i);else alert('$__nothumb');return false;\" href=\"javascript:void();\">$thumbtext</a>
<a onclick=\"return confirm('$__confirmdelete')\" href=\"".basename(__FILE__)."?action=delete&amp;attachment=$attachment&amp;all=$all&amp;start=$start&amp;post=$post\">$__delete</a>
<div id='target{$attachment_ID}' class='imagewrap left'>
<div id='popup{$attachment_ID}' class='popup'>
<a id=\"L{$attachment_ID}\" onclick=\"toggleLink({$attachment_ID});return false;\" href=\"javascript:void();\">$__attachment_on</a>
<a id=\"I{$attachment_ID}\" onclick=\"if($thumb)toggleImage({$attachment_ID});else alert('$__nothumb');return false;\" href=\"javascript:void();\">$thumbtext</a>
<a onclick=\"return confirm('$__confirmdelete')\" href=\"".basename(__FILE__)."?action=delete&amp;attachment={$attachment_ID}&amp;all=$all&amp;start=$start&amp;post=$post\">$__delete</a>
<a onclick=\"popup.style.display='none';return false;\" href=\"javascript:void()\">$__close</a>
</div>
<a id=\"link$i\" class=\"imagelink\" href=\"$href\" onclick=\"imagePopup($i);return false;\" title=\"{$image['post_title']}\">
<img id='image$i' src='$src' alt='{$image['post_title']}' $height_width />
<a id=\"{$attachment_ID}\" rel=\"attachment\" class=\"imagelink\" href=\"$href\" onclick=\"imagePopup({$attachment_ID});return false;\" title=\"{$image['post_title']}\">
<img id=\"image{$attachment_ID}\" src=\"$src\" alt=\"{$attachment_ID}\" $height_width />
</a>
</div>
";
$i++;
}
}
@ -242,7 +244,7 @@ function init() {
popup = false;
}
function toggleLink(n) {
o=document.getElementById('link'+n);
o=document.getElementById(n);
oi=document.getElementById('L'+n);
if ( oi.innerHTML == attachmenton ) {
o.href = eval('href'+n+'b');

View File

@ -622,7 +622,8 @@ function pingback($content, $post_ID) {
// We don't wanna ping first and second types, even if they have a valid <link/>
foreach($post_links_temp[0] as $link_test) :
if ( !in_array($link_test, $pung) && url_to_postid($link_test) != $post_ID) : // If we haven't pung it already and it isn't a link to itself
if ( !in_array($link_test, $pung) && (url_to_postid($link_test) != $post_ID) // If we haven't pung it already and it isn't a link to itself
&& !is_local_attachment($link_test) ) : // Also, let's never ping local attachments.
$test = parse_url($link_test);
if (isset($test['query']))
$post_links[] = $link_test;
@ -753,6 +754,18 @@ function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
return false;
}
function is_local_attachment($url) {
if ( !strstr($url, get_bloginfo('home') ) )
return false;
if ( strstr($url, get_bloginfo('home') . '/?attachment_id=') )
return true;
if ( $id = url_to_postid($url) ) {
$post = & get_post($id);
if ( 'attachment' == $post->post_status )
return true;
}
return false;
}
function wp_set_comment_status($comment_id, $comment_status) {
global $wpdb;
@ -788,7 +801,6 @@ function wp_set_comment_status($comment_id, $comment_status) {
}
}
function wp_get_comment_status($comment_id) {
global $wpdb;