743 | | * Replace hrefs of attachment anchors with up-to-date permalinks. |
744 | | * |
745 | | * @since 2.3.0 |
746 | | * @access private |
747 | | * |
748 | | * @param unknown_type $post_ID |
749 | | * @return unknown |
750 | | */ |
751 | | function _fix_attachment_links( $post_ID ) { |
752 | | global $_fix_attachment_link_id; |
753 | | |
754 | | $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 ) ) |
760 | | return; |
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 ) ) |
766 | | continue; |
767 | | |
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] ); |
796 | | } |
797 | | |
798 | | /** |