diff --git src/wp-includes/class-wp-site-query.php src/wp-includes/class-wp-site-query.php
index bc3e3519a3..5a6e015221 100644
|
|
class WP_Site_Query { |
288 | 288 | $this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this ); |
289 | 289 | } |
290 | 290 | |
291 | | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
292 | | $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
| 291 | /** |
| 292 | * Filter the sites array before the query takes place. |
| 293 | * |
| 294 | * Return a non-null value to bypass WordPress's default site queries. |
| 295 | * |
| 296 | * |
| 297 | * @since 5.2.0 |
| 298 | * |
| 299 | * @param array|null $site_ids Return an array of site data to short-circuit WP's site query, |
| 300 | * or null to allow WP to run its normal queries. |
| 301 | * @param WP_Query $this The WP_Site_Query instance, passed by reference. |
| 302 | */ |
| 303 | $site_ids = apply_filters_ref_array( 'sites_pre_query', array( null, &$this ) ); |
293 | 304 | |
294 | | // Ignore the $fields argument as the queried result will be the same regardless. |
295 | | unset( $_args['fields'] ); |
| 305 | if ( null === $site_ids ) { |
296 | 306 | |
297 | | $key = md5( serialize( $_args ) ); |
298 | | $last_changed = wp_cache_get_last_changed( 'sites' ); |
| 307 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
| 308 | $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); |
299 | 309 | |
300 | | $cache_key = "get_sites:$key:$last_changed"; |
301 | | $cache_value = wp_cache_get( $cache_key, 'sites' ); |
| 310 | // Ignore the $fields argument as the queried result will be the same regardless. |
| 311 | unset( $_args['fields'] ); |
302 | 312 | |
303 | | if ( false === $cache_value ) { |
304 | | $site_ids = $this->get_site_ids(); |
305 | | if ( $site_ids ) { |
306 | | $this->set_found_sites(); |
307 | | } |
| 313 | $key = md5( serialize( $_args ) ); |
| 314 | $last_changed = wp_cache_get_last_changed( 'sites' ); |
308 | 315 | |
309 | | $cache_value = array( |
310 | | 'site_ids' => $site_ids, |
311 | | 'found_sites' => $this->found_sites, |
312 | | ); |
313 | | wp_cache_add( $cache_key, $cache_value, 'sites' ); |
314 | | } else { |
315 | | $site_ids = $cache_value['site_ids']; |
316 | | $this->found_sites = $cache_value['found_sites']; |
| 316 | $cache_key = "get_sites:$key:$last_changed"; |
| 317 | $cache_value = wp_cache_get( $cache_key, 'sites' ); |
| 318 | |
| 319 | if ( false === $cache_value ) { |
| 320 | $site_ids = $this->get_site_ids(); |
| 321 | if ( $site_ids ) { |
| 322 | $this->set_found_sites(); |
| 323 | } |
| 324 | |
| 325 | $cache_value = array( |
| 326 | 'site_ids' => $site_ids, |
| 327 | 'found_sites' => $this->found_sites, |
| 328 | ); |
| 329 | wp_cache_add( $cache_key, $cache_value, 'sites' ); |
| 330 | } else { |
| 331 | $site_ids = $cache_value['site_ids']; |
| 332 | $this->found_sites = $cache_value['found_sites']; |
| 333 | } |
317 | 334 | } |
318 | 335 | |
319 | 336 | if ( $this->found_sites && $this->query_vars['number'] ) { |
diff --git src/wp-includes/comment.php src/wp-includes/comment.php
index 1cb9be5ada..5e89ef7695 100644
|
|
function wp_allow_comment( $commentdata, $avoid_die = false ) { |
756 | 756 | if ( $is_flood ) { |
757 | 757 | /** This filter is documented in wp-includes/comment-template.php */ |
758 | 758 | $comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) ); |
759 | | |
| 759 | |
760 | 760 | return new WP_Error( 'comment_flood', $comment_flood_message, 429 ); |
761 | 761 | } |
762 | 762 | |
diff --git tests/phpunit/tests/user/query.php tests/phpunit/tests/user/query.php
index 1330d8b5c9..d39b7d7530 100644
|
|
class Tests_User_Query extends WP_UnitTestCase { |
1723 | 1723 | |
1724 | 1724 | return array( 555 ); |
1725 | 1725 | } |
| 1726 | |
| 1727 | /** |
| 1728 | * @ticket 45749 |
| 1729 | */ |
| 1730 | public function test_sites_pre_query_filter_should_bypass_database_query() { |
| 1731 | global $wpdb; |
| 1732 | |
| 1733 | add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 ); |
| 1734 | |
| 1735 | $num_queries = $wpdb->num_queries; |
| 1736 | |
| 1737 | $q = new WP_Site_Query(); |
| 1738 | $results = $q->query( |
| 1739 | array( |
| 1740 | 'fields' => 'ids', |
| 1741 | ) |
| 1742 | ); |
| 1743 | |
| 1744 | remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 ); |
| 1745 | |
| 1746 | // Make sure no queries were executed. |
| 1747 | $this->assertSame( $num_queries, $wpdb->num_queries ); |
| 1748 | |
| 1749 | // We manually inserted a non-existing site and overrode the results with it. |
| 1750 | $this->assertSame( array( 555 ), $q->sites ); |
| 1751 | |
| 1752 | // Make sure manually setting total_users doesn't get overwritten. |
| 1753 | $this->assertEquals( 1, $q->found_sites ); |
| 1754 | } |
| 1755 | |
| 1756 | public static function filter_sites_pre_query( $sites, $query ) { |
| 1757 | $query->found_sites = 1; |
| 1758 | |
| 1759 | return array( 555 ); |
| 1760 | } |
1726 | 1761 | } |