Ticket #19225: better-attachment-url-backfilling.diff

File better-attachment-url-backfilling.diff, 1.5 KB (added by tott, 19 months ago)
Line 
1Index: wordpress-importer.php
2===================================================================
3--- wordpress-importer.php      (revision 461346)
4+++ wordpress-importer.php      (working copy)
5@@ -952,12 +952,21 @@
6                global $wpdb;
7                // make sure we do the longest urls first, in case one is a substring of another
8                uksort( $this->url_remap, array(&$this, 'cmpr_strlen') );
9-
10+               $replacements = array();
11                foreach ( $this->url_remap as $from_url => $to_url ) {
12-                       // remap urls in post_content
13-                       $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) );
14-                       // remap enclosure urls
15-                       $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) );
16+                       $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE %s", "%$from_url%" ) );
17+                       if ( count( $post_ids ) >  0 ) {
18+                               foreach ( $post_ids as $id ) {
19+                                       $replacements[$id][$from_url] = $to_url;
20+                               }
21+                       }
22+               }
23+               foreach( $replacements as $post_id => $post_replacements ) {
24+                       $post = get_post( $post_id );
25+                       uksort( $post_replacements, array(&$this, 'cmpr_strlen') );
26+                       $new_post_content = str_replace( array_keys( $post_replacements ), array_values( $post_replacements ), $post->post_content );
27+                       if ( $new_post_content <> $post->post_content )
28+                               $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET post_content = %s WHERE ID = %d", $new_post_content, $post_id ) );
29                }
30        }
31