From 3d56328372dc407f731b231836a13ca48caca33f Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Thu, 16 Jun 2016 23:08:30 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wp-site-query.php | 37 ++++++++-- tests/phpunit/tests/multisite/siteQuery.php | 78 +++++++++++++++++++++ 2 files changed, 110 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-site-query.php b/src/wp-includes/class-wp-site-query.php index e040971d96..12fa1df39c 100644 --- a/src/wp-includes/class-wp-site-query.php +++ b/src/wp-includes/class-wp-site-query.php @@ -141,6 +141,8 @@ class WP_Site_Query { * @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 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. * } */ @@ -170,6 +172,7 @@ class WP_Site_Query { 'spam' => null, 'deleted' => null, 'search' => '', + 'search_columns' => array(), 'count' => false, 'date_query' => null, // See WP_Date_Query 'update_site_cache' => true, @@ -481,10 +484,30 @@ class WP_Site_Query { // Falsey search strings are ignored. if ( strlen( $this->query_vars['search'] ) ) { - $this->sql_clauses['where']['search'] = $this->get_search_sql( - $this->query_vars['search'], - array( 'domain', 'path' ) - ); + $search_columns = array(); + + 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']; @@ -563,7 +586,11 @@ class WP_Site_Query { protected function get_search_sql( $string, $columns ) { global $wpdb; - $like = '%' . $wpdb->esc_like( $string ) . '%'; + if ( false !== strpos( $string, '*' ) ) { + $like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%'; + } else { + $like = '%' . $wpdb->esc_like( $string ) . '%'; + } $searches = array(); foreach ( $columns as $column ) { diff --git a/tests/phpunit/tests/multisite/siteQuery.php b/tests/phpunit/tests/multisite/siteQuery.php index 769638dea5..448023f97a 100644 --- a/tests/phpunit/tests/multisite/siteQuery.php +++ b/tests/phpunit/tests/multisite/siteQuery.php @@ -470,6 +470,84 @@ class Tests_Multisite_Site_Query extends WP_UnitTestCase { $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;