Make WordPress Core

Ticket #34047: 34047.diff

File 34047.diff, 5.7 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index c50a9d9..17b2878 100644
    add_filter( 'pingback_ping_source_uri', 'pingback_ping_source_uri' ); 
    201201add_filter( 'xmlrpc_pingback_error',    'xmlrpc_pingback_error'               );
    202202add_filter( 'title_save_pre',           'trim'                                );
    203203add_filter( 'get_comment_metadata',     'wp_lazyload_comment_meta',     10, 2 );
    204 add_filter( 'get_term_metadata',        'wp_lazyload_term_meta',        10, 2 );
    205204
    206205add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 );
    207206
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index 28cf56f..ef11f08 100644
    class WP_Query { 
    35403540                if ( $this->posts )
    35413541                        $this->posts = array_map( 'get_post', $this->posts );
    35423542
     3543
     3544                if ( $q['update_post_term_cache'] ) {
     3545                        add_action( 'get_term_metadata', array( $this, 'lazyload_term_meta' ), 10, 2 );
     3546                }
     3547
    35433548                if ( ! $q['suppress_filters'] ) {
    35443549                        /**
    35453550                         * Filter the raw post results array, prior to status checks.
    class WP_Query { 
    47164721                        $this->setup_postdata( $this->post );
    47174722                }
    47184723        }
     4724
     4725        /**
     4726         * Lazy-loads termmeta for located posts.
     4727         *
     4728         * As a rule, term queries (`get_terms()` and `wp_get_object_terms()`) prime the metadata cache for matched terms by
     4729         * default. However, this can cause a slight performance penalty, especially when that metadata is not actually used.
     4730         * In the context of a `WP_Query` loop, we're able to avoid this potential penalty. `update_object_term_cache()`,
     4731         * called from `update_post_caches()`, does not 'update_term_meta_cache'. Instead, the first time `get_term_meta()` is
     4732         * called from within a `WP_Query` loop, the current function detects the fact, and then primes the metadata cache for
     4733         * all terms attached to all posts in the loop, with a single database query.
     4734         *
     4735         * @since 4.4.0
     4736         *
     4737         * @param null $check   The `$check` param passed from the 'pre_term_metadata' hook.
     4738         * @param int  $term_id ID of the term whose metadata is being cached.
     4739         * @return null In order not to short-circuit `get_metadata()`.
     4740         */
     4741        public function lazyload_term_meta( $check, $term_id ) {
     4742                // We can only lazyload if the entire post object is present.
     4743                $posts = array();
     4744                foreach ( $this->posts as $post ) {
     4745                        if ( $post instanceof WP_Post ) {
     4746                                $posts[] = $post;
     4747                        }
     4748                }
     4749
     4750                if ( empty( $posts ) ) {
     4751                        return;
     4752                }
     4753
     4754                // Fetch cached term_ids for each post. Keyed by term_id for faster lookup.
     4755                $term_ids = array();
     4756                foreach ( $posts as $post ) {
     4757                        $taxonomies = get_object_taxonomies( $post->post_type );
     4758                        foreach ( $taxonomies as $taxonomy ) {
     4759                                // No extra queries. Term cache should already be primed by 'update_post_term_cache'.
     4760                                $terms = get_object_term_cache( $post->ID, $taxonomy );
     4761                                if ( false !== $terms ) {
     4762                                        foreach ( $terms as $term ) {
     4763                                                if ( ! isset( $term_ids[ $term->term_id ] ) ) {
     4764                                                        $term_ids[ $term->term_id ] = 1;
     4765                                                }
     4766                                        }
     4767                                }
     4768                        }
     4769                }
     4770
     4771                if ( $term_ids ) {
     4772                        update_termmeta_cache( array_keys( $term_ids ) );
     4773                }
     4774
     4775                return $check;
     4776        }
    47194777}
    47204778
    47214779/**
  • src/wp-includes/taxonomy-functions.php

    diff --git src/wp-includes/taxonomy-functions.php src/wp-includes/taxonomy-functions.php
    index 3d091e9..e6b9daa 100644
    function update_termmeta_cache( $term_ids ) { 
    15761576}
    15771577
    15781578/**
    1579  * Lazy-loads termmeta when inside of a `WP_Query` loop.
    1580  *
    1581  * As a rule, term queries (`get_terms()` and `wp_get_object_terms()`) prime the metadata cache for matched terms by
    1582  * default. However, this can cause a slight performance penalty, especially when that metadata is not actually used.
    1583  * In the context of a `WP_Query` loop, we're able to avoid this potential penalty. `update_object_term_cache()`,
    1584  * called from `update_post_caches()`, does not 'update_term_meta_cache'. Instead, the first time `get_term_meta()` is
    1585  * called from within a `WP_Query` loop, the current function detects the fact, and then primes the metadata cache for
    1586  * all terms attached to all posts in the loop, with a single database query.
    1587  *
    1588  * @since 4.4.0
    1589  *
    1590  * @param null $check   The `$check` param passed from the 'pre_term_metadata' hook.
    1591  * @param int  $term_id ID of the term whose metadata is being cached.
    1592  * @return null In order not to short-circuit `get_metadata()`.
    1593  */
    1594 function wp_lazyload_term_meta( $check, $term_id ) {
    1595         global $wp_query;
    1596 
    1597         if ( $wp_query instanceof WP_Query && ! empty( $wp_query->posts ) && $wp_query->get( 'update_post_term_cache' ) ) {
    1598                 // We can only lazyload if the entire post object is present.
    1599                 $posts = array();
    1600                 foreach ( $wp_query->posts as $post ) {
    1601                         if ( $post instanceof WP_Post ) {
    1602                                 $posts[] = $post;
    1603                         }
    1604                 }
    1605 
    1606                 if ( empty( $posts ) ) {
    1607                         return;
    1608                 }
    1609 
    1610                 // Fetch cached term_ids for each post. Keyed by term_id for faster lookup.
    1611                 $term_ids = array();
    1612                 foreach ( $posts as $post ) {
    1613                         $taxonomies = get_object_taxonomies( $post->post_type );
    1614                         foreach ( $taxonomies as $taxonomy ) {
    1615                                 // No extra queries. Term cache should already be primed by 'update_post_term_cache'.
    1616                                 $terms = get_object_term_cache( $post->ID, $taxonomy );
    1617                                 if ( false !== $terms ) {
    1618                                         foreach ( $terms as $term ) {
    1619                                                 if ( ! isset( $term_ids[ $term->term_id ] ) ) {
    1620                                                         $term_ids[ $term->term_id ] = 1;
    1621                                                 }
    1622                                         }
    1623                                 }
    1624                         }
    1625                 }
    1626 
    1627                 if ( $term_ids ) {
    1628                         update_termmeta_cache( array_keys( $term_ids ) );
    1629                 }
    1630         }
    1631 
    1632         return $check;
    1633 }
    1634 
    1635 /**
    16361579 * Check if Term exists.
    16371580 *
    16381581 * Formerly is_term(), introduced in 2.3.0.