Make WordPress Core

Ticket #13429: 13429-6.patch

File 13429-6.patch, 3.2 KB (added by azaozz, 13 years ago)
  • wp-admin/includes/post.php

     
    749749 * @return unknown
    750750 */
    751751function _fix_attachment_links( $post_ID ) {
    752         global $_fix_attachment_link_id;
    753 
    754752        $post = & get_post( $post_ID, ARRAY_A );
     753        $content = $post['post_content'];
     754       
     755        // quick sanity check, don't run if no pretty permalinks or post is not published
     756        if ( !get_option('permalink_structure') || $post['post_status'] != 'publish' )
     757                return;
    755758
    756         $search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie";
    757 
    758         // See if we have any rel="attachment" links
    759         if ( 0 == preg_match_all( $search, $post['post_content'], $anchor_matches, PREG_PATTERN_ORDER ) )
     759        // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero)
     760        if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) )
    760761                return;
    761762
    762         $i = 0;
    763         $search = "#[\s]+rel=(\"|')(.*?)wp-att-(\d+)\\1#i";
    764         foreach ( $anchor_matches[0] as $anchor ) {
    765                 if ( 0 == preg_match( $search, $anchor, $id_matches ) )
    766                         continue;
     763        $site_url = get_bloginfo('url');
     764        $site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s)
     765        $replace = '';
    767766
    768                 $id = (int) $id_matches[3];
     767        foreach ( $link_matches[1] as $key => $value ) {
     768                if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-')
     769                        || !preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
     770                        || !preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) )
     771                                continue;
    769772
    770                 // While we have the attachment ID, let's adopt any orphans.
    771                 $attachment = & get_post( $id, ARRAY_A );
    772                 if ( ! empty( $attachment) && ! is_object( get_post( $attachment['post_parent'] ) ) ) {
    773                         $attachment['post_parent'] = $post_ID;
    774                         // Escape data pulled from DB.
    775                         $attachment = add_magic_quotes( $attachment );
    776                         wp_update_post( $attachment );
    777                 }
     773                $quote = $url_match[1]; // the quote (single or double)
     774                $url_id = (int) $url_match[2];
     775                $rel_id = (int) $rel_match[1];
    778776
    779                 $post_search[$i] = $anchor;
    780                  $_fix_attachment_link_id = $id;
    781                 $post_replace[$i] = preg_replace_callback( "#href=(\"|')[^'\"]*\\1#", '_fix_attachment_links_replace_cb', $anchor );
    782                 ++$i;
    783         }
     777                if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false )
     778                        continue;
    784779
    785         $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] );
     780                $link = $link_matches[0][$key];
     781                $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
    786782
    787         // Escape data pulled from DB.
    788         $post = add_magic_quotes( $post);
     783                $content = str_replace( $link, $replace, $content );
     784        }
    789785
    790         return wp_update_post( $post);
    791 }
     786        if ( $replace ) {
     787                $post['post_content'] = $content;
     788                // Escape data pulled from DB.
     789                $post = add_magic_quotes($post);
    792790
    793 function _fix_attachment_links_replace_cb($match) {
    794         global $_fix_attachment_link_id;
    795         return stripslashes( 'href='.$match[1] ).get_attachment_link( $_fix_attachment_link_id ).stripslashes( $match[1] );
     791                return wp_update_post($post);
     792        }
    796793}
    797794
    798795/**