mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
Multisite: Add meta query functionality to WP_Site_Query.
After the introduction of site metadata in [42836], it should be possible to query sites by that data. Fixes #40229. git-svn-id: https://develop.svn.wordpress.org/trunk@43010 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -256,6 +256,122 @@ class Tests_Multisite_Site_Meta extends WP_UnitTestCase {
|
||||
get_site_meta( self::$site_id, 'foo', true );
|
||||
$this->assertSame( $num_queries + 1, $wpdb->num_queries);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40229
|
||||
*/
|
||||
public function test_add_site_meta_should_bust_get_sites_cache() {
|
||||
if ( ! is_site_meta_supported() ) {
|
||||
$this->markTestSkipped( 'Tests only runs with the blogmeta database table installed' );
|
||||
}
|
||||
|
||||
add_site_meta( self::$site_id, 'foo', 'bar' );
|
||||
|
||||
// Prime cache.
|
||||
$found = get_sites( array(
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'bar',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( self::$site_id ), $found );
|
||||
|
||||
add_site_meta( self::$site_id2, 'foo', 'bar' );
|
||||
|
||||
$found = get_sites( array(
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'bar',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( self::$site_id, self::$site_id2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40229
|
||||
*/
|
||||
public function test_update_site_meta_should_bust_get_sites_cache() {
|
||||
if ( ! is_site_meta_supported() ) {
|
||||
$this->markTestSkipped( 'Tests only runs with the blogmeta database table installed' );
|
||||
}
|
||||
|
||||
add_site_meta( self::$site_id, 'foo', 'bar' );
|
||||
add_site_meta( self::$site_id2, 'foo', 'baz' );
|
||||
|
||||
// Prime cache.
|
||||
$found = get_sites( array(
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'bar',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( self::$site_id ), $found );
|
||||
|
||||
update_site_meta( self::$site_id2, 'foo', 'bar' );
|
||||
|
||||
$found = get_sites( array(
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'bar',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( self::$site_id, self::$site_id2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40229
|
||||
*/
|
||||
public function test_delete_site_meta_should_bust_get_sites_cache() {
|
||||
if ( ! is_site_meta_supported() ) {
|
||||
$this->markTestSkipped( 'Tests only runs with the blogmeta database table installed' );
|
||||
}
|
||||
|
||||
add_site_meta( self::$site_id, 'foo', 'bar' );
|
||||
add_site_meta( self::$site_id2, 'foo', 'bar' );
|
||||
|
||||
// Prime cache.
|
||||
$found = get_sites( array(
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'bar',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( self::$site_id, self::$site_id2 ), $found );
|
||||
|
||||
delete_site_meta( self::$site_id2, 'foo', 'bar' );
|
||||
|
||||
$found = get_sites( array(
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'bar',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( self::$site_id ), $found );
|
||||
}
|
||||
}
|
||||
|
||||
endif;
|
||||
|
||||
@@ -878,6 +878,193 @@ if ( is_multisite() ) :
|
||||
);
|
||||
$this->assertEquals( $number_of_queries + 1, $wpdb->num_queries );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40229
|
||||
* @dataProvider data_wp_site_query_meta_query
|
||||
*/
|
||||
public function test_wp_site_query_meta_query( $query, $expected, $strict ) {
|
||||
if ( ! is_site_meta_supported() ) {
|
||||
$this->markTestSkipped( 'Tests only runs with the blogmeta database table installed' );
|
||||
}
|
||||
|
||||
add_site_meta( self::$site_ids['wordpress.org/'], 'foo', 'foo' );
|
||||
add_site_meta( self::$site_ids['wordpress.org/foo/'], 'foo', 'bar' );
|
||||
add_site_meta( self::$site_ids['wordpress.org/foo/bar/'], 'foo', 'baz' );
|
||||
add_site_meta( self::$site_ids['make.wordpress.org/'], 'bar', 'baz' );
|
||||
add_site_meta( self::$site_ids['wordpress.org/'], 'numberfoo', 1 );
|
||||
add_site_meta( self::$site_ids['wordpress.org/foo/'], 'numberfoo', 2 );
|
||||
|
||||
$query['fields'] = 'ids';
|
||||
|
||||
$q = new WP_Site_Query();
|
||||
$found = $q->query( $query );
|
||||
|
||||
foreach ( $expected as $index => $domain_path ) {
|
||||
$expected[ $index ] = self::$site_ids[ $domain_path ];
|
||||
}
|
||||
|
||||
if ( $strict ) {
|
||||
$this->assertEquals( $expected, $found );
|
||||
} else {
|
||||
$this->assertEqualSets( $expected, $found );
|
||||
}
|
||||
}
|
||||
|
||||
public function data_wp_site_query_meta_query() {
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'meta_key' => 'foo',
|
||||
),
|
||||
array(
|
||||
'wordpress.org/',
|
||||
'wordpress.org/foo/',
|
||||
'wordpress.org/foo/bar/',
|
||||
),
|
||||
false,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'meta_key' => 'foo',
|
||||
'meta_value' => 'bar',
|
||||
),
|
||||
array(
|
||||
'wordpress.org/foo/',
|
||||
),
|
||||
false,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'meta_key' => 'foo',
|
||||
'meta_value' => array( 'bar', 'baz' ),
|
||||
'meta_compare' => 'IN',
|
||||
),
|
||||
array(
|
||||
'wordpress.org/foo/',
|
||||
'wordpress.org/foo/bar/',
|
||||
),
|
||||
false,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => 'bar',
|
||||
),
|
||||
array(
|
||||
'key' => 'numberfoo',
|
||||
'value' => 2,
|
||||
'type' => 'NUMERIC',
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'wordpress.org/foo/',
|
||||
),
|
||||
false,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'meta_key' => 'foo',
|
||||
'orderby' => 'meta_value',
|
||||
'order' => 'ASC',
|
||||
),
|
||||
array(
|
||||
'wordpress.org/foo/',
|
||||
'wordpress.org/foo/bar/',
|
||||
'wordpress.org/',
|
||||
),
|
||||
true,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'meta_key' => 'foo',
|
||||
'orderby' => 'foo',
|
||||
'order' => 'ASC',
|
||||
),
|
||||
array(
|
||||
'wordpress.org/foo/',
|
||||
'wordpress.org/foo/bar/',
|
||||
'wordpress.org/',
|
||||
),
|
||||
true,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'meta_key' => 'numberfoo',
|
||||
'orderby' => 'meta_value_num',
|
||||
'order' => 'DESC',
|
||||
),
|
||||
array(
|
||||
'wordpress.org/foo/',
|
||||
'wordpress.org/',
|
||||
),
|
||||
true,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => array( 'foo', 'bar' ),
|
||||
'compare' => 'IN',
|
||||
),
|
||||
array(
|
||||
'key' => 'numberfoo',
|
||||
),
|
||||
),
|
||||
'orderby' => array( 'meta_value' => 'ASC' ),
|
||||
),
|
||||
array(
|
||||
'wordpress.org/foo/',
|
||||
'wordpress.org/',
|
||||
),
|
||||
true,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => array( 'foo', 'bar' ),
|
||||
'compare' => 'IN',
|
||||
),
|
||||
array(
|
||||
'key' => 'numberfoo',
|
||||
),
|
||||
),
|
||||
'orderby' => array( 'foo' => 'ASC' ),
|
||||
),
|
||||
array(
|
||||
'wordpress.org/foo/',
|
||||
'wordpress.org/',
|
||||
),
|
||||
true,
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'foo',
|
||||
'value' => array( 'foo', 'bar' ),
|
||||
'compare' => 'IN',
|
||||
),
|
||||
'my_subquery' => array(
|
||||
'key' => 'numberfoo',
|
||||
),
|
||||
),
|
||||
'orderby' => array( 'my_subquery' => 'DESC' ),
|
||||
),
|
||||
array(
|
||||
'wordpress.org/foo/',
|
||||
'wordpress.org/',
|
||||
),
|
||||
true,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
endif;
|
||||
|
||||
Reference in New Issue
Block a user