/**
* Used internally to get a list of network IDs matching the query vars.
*
* @since 4.6.0
* @access protected
*
* @return int|array A single count of network IDs if a count query. An array of network IDs if a full query.
*/
protected function get_network_ids()
{
$order = $this->parse_order($this->query_vars['order']);
// Disable ORDER BY with 'none', an empty array, or boolean false.
if (in_array($this->query_vars['orderby'], array('none', array(), false), true)) {
$orderby = '';
} elseif (!empty($this->query_vars['orderby'])) {
$ordersby = is_array($this->query_vars['orderby']) ? $this->query_vars['orderby'] : preg_split('/[,\\s]/', $this->query_vars['orderby']);
$orderby_array = array();
foreach ($ordersby as $_key => $_value) {
if (!$_value) {
continue;
}
if (is_int($_key)) {
$_orderby = $_value;
$_order = $order;
} else {
$_orderby = $_key;
$_order = $_value;
}
$parsed = $this->parse_orderby($_orderby);
if (!$parsed) {
continue;
}
if ('network__in' === $_orderby) {
$orderby_array[] = $parsed;
continue;
}
$orderby_array[] = $parsed . ' ' . $this->parse_order($_order);
}
$orderby = implode(', ', $orderby_array);
} else {
$orderby = "{$this->db->site}.id {$order}";
}
$number = absint($this->query_vars['number']);
$offset = absint($this->query_vars['offset']);
if (!empty($number)) {
if ($offset) {
$limits = 'LIMIT ' . $offset . ',' . $number;
} else {
$limits = 'LIMIT ' . $number;
}
}
if ($this->query_vars['count']) {
$fields = 'COUNT(*)';
} else {
$fields = "{$this->db->site}.id";
}
// Parse network IDs for an IN clause.
if (!empty($this->query_vars['network__in'])) {
$this->sql_clauses['where']['network__in'] = "{$this->db->site}.id IN ( " . implode(',', wp_parse_id_list($this->query_vars['network__in'])) . ' )';
}
// Parse network IDs for a NOT IN clause.
if (!empty($this->query_vars['network__not_in'])) {
$this->sql_clauses['where']['network__not_in'] = "{$this->db->site}.id NOT IN ( " . implode(',', wp_parse_id_list($this->query_vars['network__not_in'])) . ' )';
}
if (!empty($this->query_vars['domain'])) {
$this->sql_clauses['where']['domain'] = $this->db->prepare("{$this->db->site}.domain = %s", $this->query_vars['domain']);
}
// Parse network domain for an IN clause.
if (is_array($this->query_vars['domain__in'])) {
$this->sql_clauses['where']['domain__in'] = "{$this->db->site}.domain IN ( '" . implode("', '", $this->db->_escape($this->query_vars['domain__in'])) . "' )";
}
// Parse network domain for a NOT IN clause.
if (is_array($this->query_vars['domain__not_in'])) {
$this->sql_clauses['where']['domain__not_in'] = "{$this->db->site}.domain NOT IN ( '" . implode("', '", $this->db->_escape($this->query_vars['domain__not_in'])) . "' )";
}
if (!empty($this->query_vars['path'])) {
$this->sql_clauses['where']['path'] = $this->db->prepare("{$this->db->site}.path = %s", $this->query_vars['path']);
}
// Parse network path for an IN clause.
if (is_array($this->query_vars['path__in'])) {
$this->sql_clauses['where']['path__in'] = "{$this->db->site}.path IN ( '" . implode("', '", $this->db->_escape($this->query_vars['path__in'])) . "' )";
}
// Parse network path for a NOT IN clause.
if (is_array($this->query_vars['path__not_in'])) {
$this->sql_clauses['where']['path__not_in'] = "{$this->db->site}.path NOT IN ( '" . implode("', '", $this->db->_escape($this->query_vars['path__not_in'])) . "' )";
}
// 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("{$this->db->site}.domain", "{$this->db->site}.path"));
}
$join = '';
$where = implode(' AND ', $this->sql_clauses['where']);
$pieces = array('fields', 'join', 'where', 'orderby', 'limits', 'groupby');
/**
* Filters the network query clauses.
*
* @since 4.6.0
*
* @param array $pieces A compacted array of network query clauses.
* @param WP_Network_Query &$this Current instance of WP_Network_Query, passed by reference.
*/
$clauses = apply_filters_ref_array('networks_clauses', array(compact($pieces), &$this));
$fields = isset($clauses['fields']) ? $clauses['fields'] : '';
$join = isset($clauses['join']) ? $clauses['join'] : '';
$where = isset($clauses['where']) ? $clauses['where'] : '';
$orderby = isset($clauses['orderby']) ? $clauses['orderby'] : '';
$limits = isset($clauses['limits']) ? $clauses['limits'] : '';
$groupby = isset($clauses['groupby']) ? $clauses['groupby'] : '';
if ($where) {
$where = 'WHERE ' . $where;
}
if ($groupby) {
$groupby = 'GROUP BY ' . $groupby;
}
if ($orderby) {
$orderby = "ORDER BY {$orderby}";
}
$found_rows = '';
if (!$this->query_vars['no_found_rows']) {
$found_rows = 'SQL_CALC_FOUND_ROWS';
}
$this->sql_clauses['select'] = "SELECT {$found_rows} {$fields}";
$this->sql_clauses['from'] = "FROM {$this->db->site} {$join}";
$this->sql_clauses['groupby'] = $groupby;
$this->sql_clauses['orderby'] = $orderby;
$this->sql_clauses['limits'] = $limits;
$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
if ($this->query_vars['count']) {
return intval($this->db->get_var($this->request));
}
$network_ids = $this->db->get_col($this->request);
return array_map('intval', $network_ids);
}