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. Built from https://develop.svn.wordpress.org/trunk@37735 git-svn-id: http://core.svn.wordpress.org/trunk@37700 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
6b7f68855b
commit
b4df848b6d
|
@ -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 ) {
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '4.6-alpha-37734';
|
||||
$wp_version = '4.6-alpha-37735';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue