Ticket #13429: 13429-4.patch
File 13429-4.patch, 4.7 KB (added by , 13 years ago) |
---|
-
wp-admin/includes/post.php
229 229 230 230 wp_update_post( $post_data ); 231 231 232 // Now that we have an ID we can fix any attachment anchor hrefs233 _fix_attachment_links( $post_ID );234 235 232 wp_set_post_lock( $post_ID ); 236 233 237 234 if ( current_user_can( $ptype->cap->edit_others_posts ) ) { … … 588 585 589 586 add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID ); 590 587 591 // Now that we have an ID we can fix any attachment anchor hrefs592 _fix_attachment_links( $post_ID );593 594 588 wp_set_post_lock( $post_ID ); 595 589 596 590 return $post_ID; … … 742 736 /** 743 737 * Replace hrefs of attachment anchors with up-to-date permalinks. 744 738 * 739 * Runs when the post is published or updated using the 'wp_insert_post_data' filter. 740 * 745 741 * @since 2.3.0 746 742 * @access private 747 743 * 748 * @param unknown_type $ post_ID744 * @param unknown_type $data 749 745 * @return unknown 750 746 */ 751 function _fix_attachment_links( $post_ID ) { 752 global $_fix_attachment_link_id; 747 function _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; 753 751 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 = ''; 755 756 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; 757 760 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; 761 766 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) ) 766 772 continue; 767 773 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 ); 769 776 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 ); 783 778 } 784 779 785 $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] ); 780 if ( $replace ) 781 $data['post_content'] = $content; 786 782 787 // Escape data pulled from DB. 788 $post = add_magic_quotes( $post); 789 790 return wp_update_post( $post); 783 return $data; 791 784 } 792 785 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 798 786 /** 799 787 * Move child posts to a new parent. 800 788 * -
wp-includes/default-filters.php
194 194 add_filter( 'pings_open', '_close_comments_for_old_post', 10, 2 ); 195 195 add_filter( 'editable_slug', 'urldecode' ); 196 196 add_filter( 'nav_menu_meta_box_object', '_wp_nav_menu_meta_box_object' ); 197 add_filter( 'wp_insert_post_data', '_fix_attachment_links', 1 ); 197 198 198 199 // Actions 199 200 add_action( 'wp_head', 'wp_enqueue_scripts', 1 );