WordPress.org

Make WordPress Core

Ticket #13429: 13429-5.patch

File 13429-5.patch, 4.7 KB (added by azaozz, 6 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
    755753
    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;
    757757
    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;
     758        $site_url = get_bloginfo('url');
     759        $site_url = substr( $site_url, (int) strpos($site_url, '://') ); // remove the http(s)
     760        $replace = '';
    761761
    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 )
    766773                        continue;
    767774
    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 );
    769777
    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 );
    783779        }
    784780
    785         $post['post_content'] = str_replace( $post_search, $post_replace, $post['post_content'] );
     781        if ( $replace )
     782                $data['post_content'] = $content;
    786783
    787         // Escape data pulled from DB.
    788         $post = add_magic_quotes( $post);
    789 
    790         return wp_update_post( $post);
     784        return $data;
    791785}
    792786
    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 
    798787/**
    799788 * Move child posts to a new parent.
    800789 *
  • 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     );