Make WordPress Core

Changeset 20308


Ignore:
Timestamp:
03/28/2012 04:02:12 PM (12 years ago)
Author:
azaozz
Message:

Improve _fix_attachment_links(), replace attachment URLs with the real permalink only for published posts, re-save only when there are changes, see #13429

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/post.php

    r19999 r20308  
    750750 */
    751751function _fix_attachment_links( $post_ID ) {
    752     global $_fix_attachment_link_id;
    753 
    754752    $post = & get_post( $post_ID, ARRAY_A );
    755 
    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 ) )
     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' )
    760757        return;
    761758
    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 ) )
     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 ) )
     761        return;
     762
     763    $site_url = get_bloginfo('url');
     764    $site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s)
     765    $replace = '';
     766
     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;
     772
     773        $quote = $url_match[1]; // the quote (single or double)
     774        $url_id = (int) $url_match[2];
     775        $rel_id = (int) $rel_match[1];
     776
     777        if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false )
    766778            continue;
    767779
    768         $id = (int) $id_matches[3];
    769 
    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         }
    778 
    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     }
    784 
    785     $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] );
    786 
    787     // Escape data pulled from DB.
    788     $post = add_magic_quotes( $post);
    789 
    790     return wp_update_post( $post);
    791 }
    792 
    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] );
     780        $link = $link_matches[0][$key];
     781        $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link );
     782
     783        $content = str_replace( $link, $replace, $content );
     784    }
     785
     786    if ( $replace ) {
     787        $post['post_content'] = $content;
     788        // Escape data pulled from DB.
     789        $post = add_magic_quotes($post);
     790
     791        return wp_update_post($post);
     792    }
    796793}
    797794
Note: See TracChangeset for help on using the changeset viewer.