WordPress.org

Make WordPress Core

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

File better-attachment-url-backfilling.3.diff, 1.7 KB (added by tott, 4 years ago)
  • wordpress-importer.php

     
    950950         */ 
    951951        function backfill_attachment_urls() { 
    952952                global $wpdb; 
    953                 // make sure we do the longest urls first, in case one is a substring of another 
    954                 uksort( $this->url_remap, array(&$this, 'cmpr_strlen') ); 
    955  
     953                $replacements = $post_contents = array(); 
    956954                foreach ( $this->url_remap as $from_url => $to_url ) { 
    957                         // remap urls in post_content 
    958                         $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) ); 
    959                         // remap enclosure urls 
    960955                        $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) ); 
     956                         
     957                        $posts = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_content FROM {$wpdb->posts} WHERE post_content LIKE %s", '%' . like_escape( $from_url ) . '%' ) ); 
     958                        if ( count( $posts ) >  0 ) { 
     959                                foreach ( $posts as $post ) { 
     960                                        $replacements[$post->ID][$from_url] = $to_url; 
     961                                        $post_contents[$post->ID] = $post->post_content; 
     962                                } 
     963                        } 
     964                        unset( $posts ); 
     965                } 
     966                foreach( $replacements as $post_id => $post_replacements ) { 
     967                        // make sure we do the longest urls first, in case one is a substring of another 
     968                        uksort( $post_replacements, array(&$this, 'cmpr_strlen') ); 
     969                        $new_post_content = str_replace( array_keys( $post_replacements ), array_values( $post_replacements ), $post_contents[$post_id] ); 
     970                        if ( $new_post_content <> $post_contents[$post_id] ) 
     971                                $wpdb->update( $wpdb->posts, array( 'post_content' => $new_post_content ), array( 'ID' => $post_id ) ); 
    961972                } 
    962973        } 
    963974