Multisite: Add search column support to `WP_Site_Query`.

`domain` and/or `path` can be used to specify which column(s) should be searched.

See #35791.


git-svn-id: https://develop.svn.wordpress.org/trunk@37735 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jeremy Felt 2016-06-16 23:08:30 +00:00
parent 0cda9e306c
commit 3d56328372
2 changed files with 110 additions and 5 deletions

View File

@ -141,6 +141,8 @@ class WP_Site_Query {
* @type int $spam Limit results to spam sites. Accepts '1' or '0'. Default empty. * @type int $spam Limit results to spam sites. Accepts '1' or '0'. Default empty.
* @type int $deleted Limit results to deleted sites. Accepts '1' or '0'. Default empty. * @type int $deleted Limit results to deleted sites. Accepts '1' or '0'. Default empty.
* @type string $search Search term(s) to retrieve matching sites for. Default empty. * @type string $search Search term(s) to retrieve matching sites for. Default empty.
* @type array $search_columns Array of column names to be searched. Accepts 'domain' and 'path'.
* Default empty array.
* @type bool $update_site_cache Whether to prime the cache for found sites. Default false. * @type bool $update_site_cache Whether to prime the cache for found sites. Default false.
* } * }
*/ */
@ -170,6 +172,7 @@ class WP_Site_Query {
'spam' => null, 'spam' => null,
'deleted' => null, 'deleted' => null,
'search' => '', 'search' => '',
'search_columns' => array(),
'count' => false, 'count' => false,
'date_query' => null, // See WP_Date_Query 'date_query' => null, // See WP_Date_Query
'update_site_cache' => true, 'update_site_cache' => true,
@ -481,10 +484,30 @@ class WP_Site_Query {
// Falsey search strings are ignored. // Falsey search strings are ignored.
if ( strlen( $this->query_vars['search'] ) ) { if ( strlen( $this->query_vars['search'] ) ) {
$this->sql_clauses['where']['search'] = $this->get_search_sql( $search_columns = array();
$this->query_vars['search'],
array( 'domain', 'path' ) if ( $this->query_vars['search_columns'] ) {
); $search_columns = array_intersect( $this->query_vars['search_columns'], array( 'domain', 'path' ) );
}
if ( ! $search_columns ) {
$search_columns = array( 'domain', 'path' );
}
/**
* Filters the columns to search in a WP_Site_Query search.
*
* The default columns include 'domain' and 'path.
*
* @since 4.6.0
*
* @param array $search_columns Array of column names to be searched.
* @param string $search Text being searched.
* @param WP_Site_Query $this The current WP_Site_Query instance.
*/
$search_columns = apply_filters( 'site_search_columns', $search_columns, $this->query_vars['search'], $this );
$this->sql_clauses['where']['search'] = $this->get_search_sql( $this->query_vars['search'], $search_columns );
} }
$date_query = $this->query_vars['date_query']; $date_query = $this->query_vars['date_query'];
@ -563,7 +586,11 @@ class WP_Site_Query {
protected function get_search_sql( $string, $columns ) { protected function get_search_sql( $string, $columns ) {
global $wpdb; global $wpdb;
if ( false !== strpos( $string, '*' ) ) {
$like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%';
} else {
$like = '%' . $wpdb->esc_like( $string ) . '%'; $like = '%' . $wpdb->esc_like( $string ) . '%';
}
$searches = array(); $searches = array();
foreach ( $columns as $column ) { foreach ( $columns as $column ) {

View File

@ -470,6 +470,84 @@ class Tests_Multisite_Site_Query extends WP_UnitTestCase {
$this->assertEquals( $expected, $found ); $this->assertEquals( $expected, $found );
} }
public function test_wp_site_query_by_search_with_text_in_path_exclude_domain_from_search() {
$q = new WP_Site_Query();
$found = $q->query( array(
'fields' => 'ids',
'search' => 'make',
'search_columns' => array( 'path' ),
) );
$expected = array(
self::$site_ids['www.w.org/make/'],
);
$this->assertEquals( $expected, $found );
}
public function test_wp_site_query_by_search_with_text_in_domain_exclude_path_from_search() {
$q = new WP_Site_Query();
$found = $q->query( array(
'fields' => 'ids',
'search' => 'make',
'search_columns' => array( 'domain' ),
) );
$expected = array(
self::$site_ids['make.wordpress.org/'],
self::$site_ids['make.wordpress.org/foo/'],
);
$this->assertEquals( $expected, $found );
}
public function test_wp_site_query_by_search_with_wildcard_in_text() {
$q = new WP_Site_Query();
$found = $q->query( array(
'fields' => 'ids',
'search' => 'm*ke',
) );
$expected = array(
self::$site_ids['www.w.org/make/'],
self::$site_ids['make.wordpress.org/'],
self::$site_ids['make.wordpress.org/foo/'],
);
$this->assertEqualSets( $expected, $found );
}
public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_path_from_search() {
$q = new WP_Site_Query();
$found = $q->query( array(
'fields' => 'ids',
'search' => 'm*ke',
'search_columns' => array( 'domain' ),
) );
$expected = array(
self::$site_ids['make.wordpress.org/'],
self::$site_ids['make.wordpress.org/foo/'],
);
$this->assertEqualSets( $expected, $found );
}
public function test_wp_site_query_by_search_with_wildcard_in_text_exclude_domain_from_search() {
$q = new WP_Site_Query();
$found = $q->query( array(
'fields' => 'ids',
'search' => 'm*ke',
'search_columns' => array( 'path' ),
) );
$expected = array(
self::$site_ids['www.w.org/make/'],
);
$this->assertEqualSets( $expected, $found );
}
} }
endif; endif;