WordPress.org

Make WordPress Core

Ticket #22176: 22176.3.diff

File 22176.3.diff, 4.7 KB (added by ryan, 7 years ago)

Add context arg to clean_post_cache(). Don't update last_changed for certain contexts.

  • wp-includes/post.php

     
    22242224
    22252225        if ( is_post_type_hierarchical( $post->post_type ) && $children ) {
    22262226                foreach ( $children as $child )
    2227                         clean_post_cache( $child );
     2227                        clean_post_cache( $child, 'delete_parent' );
    22282228        }
    22292229
    22302230        wp_clear_scheduled_hook('publish_future_post', array( $postid ) );
     
    44804480 * @uses do_action() Calls 'clean_post_cache' on $id before adding children (if any).
    44814481 *
    44824482 * @param object|int $post The post object or ID to remove from the cache
     4483 * @param string $context The context in which clean_post_cache() is called. Default is empty string.
    44834484 */
    4484 function clean_post_cache( $post ) {
     4485function clean_post_cache( $post, $context = '' ) {
    44854486        global $_wp_suspend_cache_invalidation, $wpdb;
    44864487
    44874488        if ( ! empty( $_wp_suspend_cache_invalidation ) )
     
    44984499
    44994500        wp_cache_delete( 'wp_get_archives', 'general' );
    45004501
    4501         do_action( 'clean_post_cache', $post->ID, $post );
     4502        do_action( 'clean_post_cache', $post->ID, $post, $context );
    45024503
    45034504        if ( is_post_type_hierarchical( $post->post_type ) )
    45044505                wp_cache_delete( 'get_pages', 'posts' );
     
    45074508                wp_cache_delete( 'all_page_ids', 'posts' );
    45084509                do_action( 'clean_page_cache', $post->ID );
    45094510        }
     4511
     4512        // Increment last_changed to invalidate query caches except for contexts
     4513        // where clean_post_cache() has already been called for this page load
     4514        // and contexts which do not affect query results.
     4515        if ( ! ( 'delete_parent' == $context || 'update_comment_count' == $context ) ) {
     4516                if ( function_exists( 'wp_cache_incr' ) ) {
     4517                        wp_cache_incr( 'last_changed', 1, 'posts' );
     4518                } else {
     4519                        $last_changed = wp_cache_get( 'last_changed', 'posts' );
     4520                        wp_cache_set( 'last_changed', $last_changed + 1, 'posts' );
     4521                }
     4522        }
    45104523}
    45114524
    45124525/**
  • wp-includes/comment.php

     
    16181618        $new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
    16191619        $wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
    16201620
    1621         clean_post_cache( $post );
     1621        clean_post_cache( $post, 'update_comment_count' );
    16221622
    16231623        do_action('wp_update_comment_count', $post_id, $new, $old);
    16241624        do_action('edit_post', $post_id, $post);
  • wp-includes/query.php

     
    12791279         */
    12801280         var $thumbnails_cached = false;
    12811281
     1282         /**
     1283          * The cache key for the posts_request_ids query cache.
     1284          *
     1285          * @since 3.6.0
     1286          * @access private
     1287          * @var string
     1288          */
     1289         private $cache_key;
     1290
    12821291        /**
    12831292         * Resets query flags to false.
    12841293         *
     
    26612670
    26622671                        $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
    26632672
    2664                         $ids = $wpdb->get_col( $this->request );
     2673                        $last_changed = wp_cache_get( 'last_changed', 'posts' );
     2674                        if ( ! $last_changed )
     2675                                $last_changed = wp_cache_set( 'last_changed', 1, 'posts' );
     2676                        $this->cache_key = md5( $this->request ) . $last_changed;
    26652677
     2678                        if ( ! $ids = wp_cache_get( $this->cache_key, 'posts' ) ) {
     2679                                $ids = $wpdb->get_col( $this->request );
     2680                                wp_cache_set( $this->cache_key, $ids, 'posts' );
     2681                        }
     2682
    26662683                        if ( $ids ) {
    26672684                                $this->posts = $ids;
    26682685                                $this->set_found_posts( $q, $limits );
     
    28022819                if ( $q['no_found_rows'] || ! $this->posts )
    28032820                        return;
    28042821
    2805                 if ( ! empty( $limits ) )
    2806                         $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
    2807                 else
    2808                         $this->found_posts = count( $this->posts );
     2822                if ( ! $this->cache_key || ! $this->found_posts = wp_cache_get( $this->cache_key . '_found', 'posts' ) ) {
     2823                        if ( ! empty( $limits ) )
     2824                                $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
     2825                        else
     2826                                $this->found_posts = count( $this->posts );
    28092827
    2810                 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
     2828                        $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
    28112829
     2830                        wp_cache_set( $this->cache_key . '_found', $this->found_posts, 'posts' );
     2831                } else {
     2832                        $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
     2833                }
     2834
    28122835                if ( ! empty( $limits ) )
    28132836                        $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
    28142837        }