Ticket #13429: 13429-5.patch
File 13429-5.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 755 753 756 $search = "#<a[^>]+rel=('|\")[^'\"]*attachment[^>]*>#ie"; 754 // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero) 755 if ( !strpos($content, '?attachment_id=') || !preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) ) 756 return $data; 757 757 758 // See if we have any rel="attachment" links759 if ( 0 == preg_match_all( $search, $post['post_content'], $anchor_matches, PREG_PATTERN_ORDER ))760 return;758 $site_url = get_bloginfo('url'); 759 $site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s) 760 $replace = ''; 761 761 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 ) ) 762 foreach ( $link_matches[1] as $key => $value ) { 763 if ( !strpos($value, '?attachment_id=') || !strpos($value, 'wp-att-') 764 || !preg_match( '/href=(\\\\?["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match ) 765 || !preg_match( '/rel=\\\\?["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) ) 766 continue; 767 768 $quote = $url_match[1]; // the quote (single or double) together with the slash 769 $url_id = (int) $url_match[2]; 770 $rel_id = (int) $rel_match[1]; 771 772 if ( !$url_id || !$rel_id || $url_id != $rel_id || strpos($url_match[0], $site_url) === false ) 766 773 continue; 767 774 768 $id = (int) $id_matches[3]; 775 $orig_value = $link_matches[0][$key]; 776 $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $orig_value ); 769 777 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; 778 $content = str_replace( $orig_value, $replace, $content ); 783 779 } 784 780 785 $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] ); 781 if ( $replace ) 782 $data['post_content'] = $content; 786 783 787 // Escape data pulled from DB. 788 $post = add_magic_quotes( $post); 789 790 return wp_update_post( $post); 784 return $data; 791 785 } 792 786 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 787 /** 799 788 * Move child posts to a new parent. 800 789 * -
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 );