Make WordPress Core

Ticket #21120: 21120.3.patch

File 21120.3.patch, 2.9 KB (added by kurtpayne, 12 years ago)

Adding an "update_author_cache" query var to WP_Query

  • wp-admin/includes/class-wp-posts-list-table.php

     
    8181        function prepare_items() {
    8282                global $post_type_object, $avail_post_stati, $wp_query, $per_page, $mode;
    8383
     84                add_action( 'pre_get_posts', array( $this, 'enable_author_cache' ) );
    8485                $avail_post_stati = wp_edit_posts_query();
     86                remove_action( 'pre_get_posts' , array( $this, 'enable_author_cache' ) );
    8587
    8688                $this->hierarchical_display = ( $post_type_object->hierarchical && 'menu_order title' == $wp_query->query['orderby'] );
    8789
     
    10551057                </tbody></table></form>
    10561058<?php
    10571059        }
     1060
     1061        /**
     1062         * Enable author caching
     1063         * @param WP_Query $query
     1064         */
     1065        function enable_author_cache( $query ) {
     1066                $query->set( 'update_author_cache', true );
     1067        }
    10581068}
  • wp-includes/post.php

     
    53515351                update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
    53525352        }
    53535353}
     5354
     5355/**
     5356 * Prime the user cache
     5357 *
     5358 * @since 3.5.0
     5359 *
     5360 * @access private
     5361 *
     5362 * @param array $user_ids
     5363 */
     5364function _prime_user_cache( $user_ids ) {
     5365        foreach ( $user_ids as $user_id ) {
     5366
     5367                // Skip if the user is cached already
     5368                $user = wp_cache_get( $user_id, 'users' );
     5369                if ( !empty( $user ) && !empty( $user->caps ) )
     5370                        continue;
     5371
     5372                // Get and cache the user
     5373                $userdata = WP_User::get_data_by( 'id', $user_id );
     5374                $user = new WP_User;
     5375                $user->init( $userdata );
     5376                update_user_caches( $user );
     5377                wp_cache_set( $userdata->ID, $user, 'users' );
     5378        }
     5379}
  • wp-includes/pluggable.php

     
    138138        if ( !$userdata )
    139139                return false;
    140140
     141        $user = wp_cache_get( $userdata->ID, 'users' );
     142
     143        if ( !empty( $user ) && !empty( $user->caps ) )
     144                return $user;
     145
    141146        $user = new WP_User;
    142147        $user->init( $userdata );
    143148
     149        wp_cache_set( $userdata->ID, $user, 'users' );
    144150        return $user;
    145151}
    146152endif;
  • wp-includes/query.php

     
    26512651                                $this->found_posts = $this->max_num_pages = 0;
    26522652                                $this->posts = array();
    26532653                        }
     2654
     2655                        if ( isset( $this->query_vars['update_author_cache'] ) && ( $this->query_vars['update_author_cache'] ) ) {
     2656                                $authors = array_unique( wp_list_pluck( $this->posts, 'post_author' ) );
     2657                                _prime_user_cache( $authors );
     2658                        }
     2659
    26542660                } else {
    26552661                        $this->posts = $wpdb->get_results( $this->request );
    26562662                        $this->set_found_posts( $q, $limits );