Make WordPress Core

Ticket #13429: 13429-4.patch

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

     
    229229
    230230        wp_update_post( $post_data );
    231231
    232         // Now that we have an ID we can fix any attachment anchor hrefs
    233         _fix_attachment_links( $post_ID );
    234 
    235232        wp_set_post_lock( $post_ID );
    236233
    237234        if ( current_user_can( $ptype->cap->edit_others_posts ) ) {
     
    588585
    589586        add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );
    590587
    591         // Now that we have an ID we can fix any attachment anchor hrefs
    592         _fix_attachment_links( $post_ID );
    593 
    594588        wp_set_post_lock( $post_ID );
    595589
    596590        return $post_ID;
     
    742736/**
    743737 * Replace hrefs of attachment anchors with up-to-date permalinks.
    744738 *
     739 * Runs when the post is published or updated using the 'wp_insert_post_data' filter.
     740 *
    745741 * @since 2.3.0
    746742 * @access private
    747743 *
    748  * @param unknown_type $post_ID
     744 * @param unknown_type $data
    749745 * @return unknown
    750746 */
    751 function _fix_attachment_links( $post_ID ) {
    752         global $_fix_attachment_link_id;
     747function _fix_attachment_links( $data ) {
     748        // quick sanity check, don't run if no pretty permalinks or post is not published
     749        if ( !is_array($data) || !get_option('permalink_structure') || $data['post_status'] != 'publish' )
     750                return $data;
    753751
    754         $post = & get_post( $post_ID, ARRAY_A );
     752        $content = $data['post_content']; // expected slashed
     753        $site_url = get_bloginfo('url');
     754        $site_url = substr( $site_url, strpos($site_url, '/') ); // remove the protocol
     755        $replace = '';
    755756
    756         $search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie";
     757        // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero)
     758        if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) )
     759                return $data;
    757760
    758         // See if we have any rel="attachment" links
    759         if ( 0 == preg_match_all( $search, $post['post_content'], $anchor_matches, PREG_PATTERN_ORDER ) )
    760                 return;
     761        foreach ( $link_matches[1] as $key => $value ) {
     762                if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-')
     763                        || !preg_match( '/href=(\\\\?["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match )
     764                        || !preg_match( '/rel=\\\\?["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) )
     765                                continue;
    761766
    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 ) )
     767                $quote = $url_match[1]; // the quote (single or double) together with the slash
     768                $url_id = (int) $url_match[2];
     769                $rel_id = (int) $rel_match[1];
     770
     771                if ( !$url_id || !$rel_id || $url_id != $rel_id || !strpos($value, $site_url) )
    766772                        continue;
    767773
    768                 $id = (int) $id_matches[3];
     774                $orig_value = $link_matches[0][$key];
     775                $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $orig_value );
    769776
    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;
     777                $content = str_replace( $orig_value, $replace, $content );
    783778        }
    784779
    785         $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] );
     780        if ( $replace )
     781                $data['post_content'] = $content;
    786782
    787         // Escape data pulled from DB.
    788         $post = add_magic_quotes( $post);
    789 
    790         return wp_update_post( $post);
     783        return $data;
    791784}
    792785
    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] );
    796 }
    797 
    798786/**
    799787 * Move child posts to a new parent.
    800788 *
  • wp-includes/default-filters.php

     
    194194add_filter( 'pings_open',               '_close_comments_for_old_post', 10, 2 );
    195195add_filter( 'editable_slug',            'urldecode'                           );
    196196add_filter( 'nav_menu_meta_box_object', '_wp_nav_menu_meta_box_object'        );
     197add_filter( 'wp_insert_post_data',      '_fix_attachment_links',        1     );
    197198
    198199// Actions
    199200add_action( 'wp_head',             'wp_enqueue_scripts',              1     );