Ticket #34047: 34047.2.diff
File 34047.2.diff, 6.1 KB (added by , 10 years ago) |
---|
-
src/wp-includes/default-filters.php
diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index c50a9d9..17b2878 100644
a b add_filter( 'pingback_ping_source_uri', 'pingback_ping_source_uri' ); 201 201 add_filter( 'xmlrpc_pingback_error', 'xmlrpc_pingback_error' ); 202 202 add_filter( 'title_save_pre', 'trim' ); 203 203 add_filter( 'get_comment_metadata', 'wp_lazyload_comment_meta', 10, 2 ); 204 add_filter( 'get_term_metadata', 'wp_lazyload_term_meta', 10, 2 );205 204 206 205 add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 ); 207 206 -
src/wp-includes/query.php
diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 28cf56f..956ca00 100644
a b class WP_Query { 3540 3540 if ( $this->posts ) 3541 3541 $this->posts = array_map( 'get_post', $this->posts ); 3542 3542 3543 3544 if ( $q['update_post_term_cache'] ) { 3545 add_action( 'get_term_metadata', array( $this, 'lazyload_term_meta' ), 10, 2 ); 3546 } 3547 3543 3548 if ( ! $q['suppress_filters'] ) { 3544 3549 /** 3545 3550 * Filter the raw post results array, prior to status checks. … … class WP_Query { 4716 4721 $this->setup_postdata( $this->post ); 4717 4722 } 4718 4723 } 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 4729 * terms by default. However, this can cause a slight performance penalty, especially when that metadata is 4730 * not actually used. In the context of a `WP_Query` instance, we're able to avoid this potential penalty. 4731 * `update_object_term_cache()`, called from `update_post_caches()`, does not 'update_term_meta_cache'. 4732 * Instead, the first time `get_term_meta()` is called from within a `WP_Query` loop, the current method 4733 * detects the fact, and then primes the metadata cache for all terms attached to all posts in the loop, 4734 * with a single database query. 4735 * 4736 * This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it 4737 * directly, from either inside or outside the `WP_Query` object. 4738 * 4739 * @since 4.4.0 4740 * @access public 4741 * 4742 * @param null $check The `$check` param passed from the 'pre_term_metadata' hook. 4743 * @param int $term_id ID of the term whose metadata is being cached. 4744 * @return mixed In order not to short-circuit `get_metadata()`. Generally, this is `null`, but it could be 4745 * another value if filtered by a plugin. 4746 */ 4747 public function lazyload_term_meta( $check, $term_id ) { 4748 // We can only lazyload if the entire post object is present. 4749 $posts = array(); 4750 foreach ( $this->posts as $post ) { 4751 if ( $post instanceof WP_Post ) { 4752 $posts[] = $post; 4753 } 4754 } 4755 4756 if ( ! empty( $posts ) ) { 4757 // Fetch cached term_ids for each post. Keyed by term_id for faster lookup. 4758 $term_ids = array(); 4759 foreach ( $posts as $post ) { 4760 $taxonomies = get_object_taxonomies( $post->post_type ); 4761 foreach ( $taxonomies as $taxonomy ) { 4762 // Term cache should already be primed by 'update_post_term_cache'. 4763 $terms = get_object_term_cache( $post->ID, $taxonomy ); 4764 if ( false !== $terms ) { 4765 foreach ( $terms as $term ) { 4766 if ( ! isset( $term_ids[ $term->term_id ] ) ) { 4767 $term_ids[ $term->term_id ] = 1; 4768 } 4769 } 4770 } 4771 } 4772 } 4773 4774 if ( $term_ids ) { 4775 update_termmeta_cache( array_keys( $term_ids ) ); 4776 } 4777 } 4778 4779 // We only do this once per `WP_Query` instance. 4780 remove_action( 'get_term_metadata', array( $this, 'lazyload_term_meta' ), 10, 2 ); 4781 return $check; 4782 } 4719 4783 } 4720 4784 4721 4785 /** -
src/wp-includes/taxonomy-functions.php
diff --git a/src/wp-includes/taxonomy-functions.php b/src/wp-includes/taxonomy-functions.php index 3d091e9..e6b9daa 100644
a b function update_termmeta_cache( $term_ids ) { 1576 1576 } 1577 1577 1578 1578 /** 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 by1582 * 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()` is1585 * called from within a `WP_Query` loop, the current function detects the fact, and then primes the metadata cache for1586 * all terms attached to all posts in the loop, with a single database query.1587 *1588 * @since 4.4.01589 *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 /**1636 1579 * Check if Term exists. 1637 1580 * 1638 1581 * Formerly is_term(), introduced in 2.3.0.