Ticket #5557: defer-comment-counts-r6528-3.patch

File defer-comment-counts-r6528-3.patch, 3.7 KB (added by tellyworth, 4 years ago)

fix a poor assumption with comment_exists() change

  • wordpress/wp-includes/comment.php

     
    491491        return $rval; 
    492492} 
    493493 
     494function wp_defer_comment_counting($defer=NULL) { 
     495        static $_defer = false; 
     496         
     497        if ( is_bool($defer) ) { 
     498                $_defer = $defer; 
     499                // flush any deferred counts 
     500                if ( !$defer ) 
     501                        wp_update_comment_count( NULL, true ); 
     502        } 
     503         
     504        return $_defer; 
     505} 
    494506 
    495 function wp_update_comment_count($post_id) { 
     507function wp_update_comment_count($post_id, $do_deferred=false) { 
     508        static $_deferred = array(); 
     509         
     510        if ( $do_deferred ) { 
     511                $_deferred = array_unique($_deferred); 
     512                foreach ( $_deferred as $i => $_post_id ) { 
     513                        wp_update_comment_count_now($_post_id); 
     514                        unset( $_deferred[$i] ); 
     515                } 
     516        } 
     517         
     518        if ( wp_defer_comment_counting() ) { 
     519                $_deferred[] = $post_id; 
     520                return true; 
     521        } 
     522        elseif ( $post_id ) { 
     523                return wp_update_comment_count_now($post_id); 
     524        } 
     525                 
     526} 
     527 
     528function wp_update_comment_count_now($post_id) { 
    496529        global $wpdb; 
    497530        $post_id = (int) $post_id; 
    498531        if ( !$post_id ) 
  • wordpress/wp-admin/import/wordpress.php

     
    376376                        $cat_index++; 
    377377                } 
    378378 
    379                 if ($post_id = post_exists($post_title, '', $post_date)) { 
     379                $post_exists = post_exists($post_title, '', $post_date); 
     380                 
     381                if ( $post_exists ) { 
    380382                        echo '<li>'; 
    381383                        printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title)); 
    382384                } else { 
     
    475477                        $comment_type         = $this->get_tag( $comment, 'wp:comment_type'); 
    476478                        $comment_parent       = $this->get_tag( $comment, 'wp:comment_parent'); 
    477479 
    478                         if ( !comment_exists($comment_author, $comment_date) ) { 
     480                        // if this is a new post we can skip the comment_exists() check 
     481                        if ( !$post_exists || !comment_exists($comment_author, $comment_date) ) { 
    479482                                $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_approved', 'comment_type', 'comment_parent'); 
    480483                                wp_insert_comment($commentdata); 
    481484                                $num_comments++; 
     
    648651                return apply_filters('import_attachment_size_limit', 0); 
    649652        } 
    650653         
     654        function import_start() { 
     655                wp_defer_term_counting(true); 
     656                wp_defer_comment_counting(true); 
     657                do_action('import_start'); 
     658        } 
     659         
     660        function import_end() { 
     661                do_action('import_end'); 
     662                 
     663                // clear the caches after backfilling 
     664                foreach ($this->post_ids_processed as $post_id) 
     665                        clean_post_cache($post_id); 
     666                 
     667                wp_defer_term_counting(false); 
     668                wp_defer_comment_counting(false); 
     669        } 
    651670 
    652671        function import($id, $fetch_attachments = false) { 
    653672                $this->id = (int) $id; 
    654673                $this->fetch_attachments = ($this->allow_fetch_attachments() && (bool) $fetch_attachments); 
    655674 
    656675                add_filter('import_post_meta_key', array($this, 'is_valid_meta_key')); 
    657                 do_action('import_start'); 
    658676                $file = get_attached_file($this->id); 
    659677                $this->import_file($file); 
    660                 do_action('import_end'); 
    661678        } 
    662679                 
    663680        function import_file($file) { 
    664681                $this->file = $file; 
    665682                 
     683                $this->import_start(); 
    666684                $this->get_authors_from_post(); 
    667                 wp_defer_term_counting(true); 
    668685                $this->get_entries(); 
    669686                $this->process_categories(); 
    670687                $this->process_tags(); 
    671688                $result = $this->process_posts(); 
    672689                $this->backfill_parents(); 
    673690                $this->backfill_attachment_urls(); 
     691                $this->import_end(); 
    674692                 
    675                 // clear the caches after backfilling 
    676                 foreach ($this->post_ids_processed as $post_id) 
    677                         clean_post_cache($post_id); 
    678                  
    679                 wp_defer_term_counting(false); 
    680693                if ( is_wp_error( $result ) ) 
    681694                        return $result; 
    682695        }