From 7a90a61c9dfa4dba26ad033a7659b76a6ead6e5b Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Fri, 18 Nov 2005 09:25:47 +0000 Subject: [PATCH] Attachment cleanups from skeltoac. fixes #1870 git-svn-id: https://develop.svn.wordpress.org/trunk@3145 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/admin-functions.php | 42 ++++++++++++++++++++++++++++++- wp-admin/admin-header.php | 2 +- wp-admin/inline-uploading.php | 40 +++++++++++++++-------------- wp-includes/comment-functions.php | 16 ++++++++++-- 4 files changed, 77 insertions(+), 23 deletions(-) diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index f375368483..36d3f5bb5e 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -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 = "#]+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); } -?> \ No newline at end of file +?> diff --git a/wp-admin/admin-header.php b/wp-admin/admin-header.php index ecdd34758a..93556622bd 100644 --- a/wp-admin/admin-header.php +++ b/wp-admin/admin-header.php @@ -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" }); diff --git a/wp-admin/inline-uploading.php b/wp-admin/inline-uploading.php index ea37da78a7..239a856819 100644 --- a/wp-admin/inline-uploading.php +++ b/wp-admin/inline-uploading.php @@ -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 .= " -
-