From 9164c3476ff941274ddee2a6a2f57034b9ad8dd5 Mon Sep 17 00:00:00 2001 From: John James Jacoby Date: Tue, 6 Dec 2016 23:03:35 -0600 Subject: [PATCH] Check count of `__in` queries, and use singular type if 1. 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. --- .../classes/class-wp-site-alias-query.php | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/wp-site-aliases/includes/classes/class-wp-site-alias-query.php b/wp-site-aliases/includes/classes/class-wp-site-alias-query.php index 74480a0..4c4f603 100644 --- a/wp-site-aliases/includes/classes/class-wp-site-alias-query.php +++ b/wp-site-aliases/includes/classes/class-wp-site-alias-query.php @@ -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. @@ -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. @@ -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. @@ -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. @@ -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.