Skip to content

Commit

Permalink
Check count of __in queries, and use singular type if 1.
Browse files Browse the repository at this point in the history
Since queries are cached by the `md5()` of their vars, this ensures that `IN` look-ups of only 1 domain are cache hits when 1 domain is both intentionally and inadvertently looked up.
  • Loading branch information
JJJ committed Dec 7, 2016
1 parent df8cacd commit 9164c34
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions wp-site-aliases/includes/classes/class-wp-site-alias-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,11 @@ protected function get_alias_ids() {

// Parse site alias IDs for an IN clause.
if ( ! empty( $this->query_vars['alias__in'] ) ) {
$this->sql_clauses['where']['alias__in'] = "ba.id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
if ( 1 === count( $this->query_vars['alias__in'] ) ) {
$this->sql_clauses['where']['ID'] = $this->db->prepare( 'ba.id = %d', reset( $this->query_vars['alias__in'] ) );
} else {
$this->sql_clauses['where']['alias__in'] = "ba.id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['alias__in'] ) ) . ' )';
}
}

// Parse site alias IDs for a NOT IN clause.
Expand All @@ -432,7 +436,11 @@ protected function get_alias_ids() {

// Parse site IDs for an IN clause.
if ( ! empty( $this->query_vars['site__in'] ) ) {
$this->sql_clauses['where']['site__in'] = "ba.blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
if ( 1 === count( $this->query_vars['site__in'] ) ) {
$this->sql_clauses['where']['site_id'] = $this->db->prepare( 'ba.blog_id = %d', reset( $this->query_vars['site__in'] ) );
} else {
$this->sql_clauses['where']['site__in'] = "ba.blog_id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['site__in'] ) ) . ' )';
}
}

// Parse site IDs for a NOT IN clause.
Expand All @@ -448,7 +456,11 @@ protected function get_alias_ids() {

// Parse site alias domain for an IN clause.
if ( is_array( $this->query_vars['domain__in'] ) ) {
$this->sql_clauses['where']['domain__in'] = "ba.domain IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__in'] ) ) . "' )";
if ( 1 === count( $this->query_vars['domain__in'] ) ) {
$this->sql_clauses['where']['domain'] = $this->db->prepare( 'ba.domain = %s', reset( $this->query_vars['domain__in'] ) );
} else {
$this->sql_clauses['where']['domain__in'] = "ba.domain IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__in'] ) ) . "' )";
}
}

// Parse site alias domain for a NOT IN clause.
Expand All @@ -464,7 +476,11 @@ protected function get_alias_ids() {

// Parse site alias status for an IN clause.
if ( is_array( $this->query_vars['status__in'] ) ) {
$this->sql_clauses['where']['status__in'] = "ba.status IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['status__in'] ) ) . "' )";
if ( 1 === count( $this->query_vars['status__in'] ) ) {
$this->sql_clauses['where']['status'] = $this->db->prepare( 'ba.status = %s', reset( $this->query_vars['status__in'] ) );
} else {
$this->sql_clauses['where']['status__in'] = "ba.status IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['status__in'] ) ) . "' )";
}
}

// Parse site alias status for a NOT IN clause.
Expand All @@ -480,7 +496,11 @@ protected function get_alias_ids() {

// Parse site alias type for an IN clause.
if ( is_array( $this->query_vars['type__in'] ) ) {
$this->sql_clauses['where']['type__in'] = "ba.type IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['type__in'] ) ) . "' )";
if ( 1 === count( $this->query_vars['type__in'] ) ) {
$this->sql_clauses['where']['type'] = $this->db->prepare( 'ba.type = %s', reset( $this->query_vars['type__in'] ) );
} else {
$this->sql_clauses['where']['type__in'] = "ba.type IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['type__in'] ) ) . "' )";
}
}

// Parse site alias type for a NOT IN clause.
Expand Down

0 comments on commit 9164c34

Please sign in to comment.