Ticket #9951: query.2.diff

File query.2.diff, 8.6 KB (added by willmot, 3 years ago)

Removed unneeded $join

  • query.php

     
    13961396                                $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']); 
    13971397                                $this->is_tag = true; 
    13981398                        } 
     1399                         
     1400                        // Custom Taxonomies 
     1401                        $this->custom_taxonomies = array(); 
     1402                        $this->custom_taxonomies__not_in = array(); 
     1403                         
     1404                        // loop through registered taxonomies to check for respective query vars 
     1405                        foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) { 
     1406                                if( $taxonomy == 'category' || $taxonomy == 'post_tag' || $taxonomy == 'link_category' ) continue; 
     1407                                if( array_key_exists( $taxonomy . '__in', $qv ) ) { 
     1408                                        $is = 'is_' . $taxonomy; 
     1409                                        $this->$is = true; 
     1410                                        $this->custom_taxonomies[$taxonomy . '__in'] = $taxonomy; 
     1411                                } 
     1412                                elseif( array_key_exists( $taxonomy . '__not_in', $qv ) ) { 
     1413                                        $is = 'is_' . $taxonomy; 
     1414                                        $this->$is = true; 
     1415                                        $this->custom_taxonomies[$taxonomy . '__not_in'] = $taxonomy; 
     1416                                } 
     1417                        } 
    13991418 
    14001419                        if ( empty($qv['taxonomy']) || empty($qv['term']) ) { 
    14011420                                $this->is_tax = false; 
     
    15961615                        if ( $this->is_search ) 
    15971616                                $q['post_type'] = 'any'; 
    15981617                        else 
    1599                                 $q['post_type'] = ''; 
     1618                                $q['post_type'] = 'post'; 
    16001619                } 
    16011620                $post_type = $q['post_type']; 
    16021621                if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 ) 
     
    17561775                                $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))"; 
    17571776                                $searchand = ' AND '; 
    17581777                        } 
    1759                         $term = esc_sql($q['s']); 
     1778                        $term = $wpdb->escape($q['s']); 
    17601779                        if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] ) 
    17611780                                $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')"; 
    17621781 
     
    19021921                                        $whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')"; 
    19031922                        } 
    19041923                } 
     1924                 
     1925                //Custom Taxonomies 
     1926                foreach( (array) $this->custom_taxonomies as $var => $custom_tax ) { 
     1927                        $reqtag = is_term( $q[$var][0], $custom_tax ); 
     1928                        $distinct = 'DISTINCT'; 
     1929                } 
     1930                 
     1931                //Custom Taxonomy Not In 
     1932                foreach( (array) $this->custom_taxonomies__not_in as $var => $custom_tax ) { 
     1933                        if ( $wpdb->has_cap( 'subqueries' ) ) { 
     1934                                $tax_string = "'" . implode("', '", $q[$var]) . "'"; 
     1935                                $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = '$custom_tax' AND tt.term_id IN ($tax_string) )"; 
     1936                        } else { 
     1937                                $ids = get_objects_in_term($q[$var], $custom_tax); 
     1938                                if ( !is_wp_error($ids) && is_array($ids) && count($ids) > 0 ) 
     1939                                        $whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')"; 
     1940                        } 
     1941                } 
    19051942 
    1906                 // Tag and slug intersections. 
     1943                // Tag and slug intersections. Also merge with custom taxonomies 
    19071944                $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag'); 
     1945                $intersections = array_merge( $intersections, (array) $this->custom_taxonomies ); 
    19081946                $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below 
    19091947                foreach ($intersections as $item => $taxonomy) { 
    19101948                        if ( empty($q[$item]) ) continue; 
    1911                         if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used 
    1912  
     1949                                                 
    19131950                        if ( $item != 'category__and' ) { 
    19141951                                $reqtag = is_term( $q[$item][0], 'post_tag' ); 
    19151952                                if ( !empty($reqtag) ) 
    19161953                                        $q['tag_id'] = $reqtag['term_id']; 
    19171954                        } 
    19181955 
    1919                         if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) ) 
    1920                                 $taxonomy_field = 'slug'; 
    1921                         else 
    1922                                 $taxonomy_field = 'term_id'; 
     1956                        $taxonomy_field = $item != ('tag_slug__and' || 'tag_slug__in') ? 'slug' : 'term_id'; 
    19231957 
    19241958                        $q[$item] = array_unique($q[$item]); 
    19251959                        $tsql = "SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN $wpdb->terms t ON (tt.term_id = t.term_id)"; 
    1926                         $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')"; 
     1960                        $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";                     
    19271961                        if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship 
    19281962                                $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]); 
    19291963                        } 
     
    20702104                                $q['orderby'] = "$wpdb->posts.post_date ".$q['order']; 
    20712105                } 
    20722106 
    2073                 $post_type_cap = $post_type; 
    2074  
    20752107                if ( 'any' == $post_type ) { 
    20762108                        $where .= " AND $wpdb->posts.post_type != 'revision'"; 
    2077                 } elseif ( ! empty( $post_type ) ) { 
    2078                         $where .= " AND $wpdb->posts.post_type = '$post_type'"; 
    20792109                } elseif ( $this->is_attachment ) { 
    20802110                        $where .= " AND $wpdb->posts.post_type = 'attachment'"; 
    2081                         $post_type_cap = 'post'; 
    20822111                } elseif ($this->is_page) { 
    20832112                        $where .= " AND $wpdb->posts.post_type = 'page'"; 
    2084                         $post_type_cap = 'page'; 
     2113                } elseif ($this->is_single) { 
     2114                        $where .= " AND $wpdb->posts.post_type = 'post'"; 
    20852115                } else { 
    2086                         $where .= " AND $wpdb->posts.post_type = 'post'"; 
    2087                         $post_type_cap = 'post'; 
     2116                        $where .= " AND $wpdb->posts.post_type = '$post_type'"; 
    20882117                } 
    20892118 
    20902119                if ( isset($q['post_status']) && '' != $q['post_status'] ) { 
     
    21042133                                $p_status[] = "$wpdb->posts.post_status = 'private'"; 
    21052134                        if ( in_array( 'publish', $q_status ) ) 
    21062135                                $r_status[] = "$wpdb->posts.post_status = 'publish'"; 
    2107                         if ( in_array( 'trash', $q_status ) ) 
    2108                                 $r_status[] = "$wpdb->posts.post_status = 'trash'"; 
    21092136 
    21102137                        if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) { 
    21112138                                $r_status = array_merge($r_status, $p_status); 
     
    21132140                        } 
    21142141 
    21152142                        if ( !empty($r_status) ) { 
    2116                                 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type_cap}s") ) 
     2143                                if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type}s") ) 
    21172144                                        $statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $r_status ) . "))"; 
    21182145                                else 
    21192146                                        $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")"; 
    21202147                        } 
    21212148                        if ( !empty($p_status) ) { 
    2122                                 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type_cap}s") ) 
     2149                                if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type}s") ) 
    21232150                                        $statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $p_status ) . "))"; 
    21242151                                else 
    21252152                                        $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")"; 
     
    21382165                                $where .= " OR $wpdb->posts.post_status = 'future' OR $wpdb->posts.post_status = 'draft' OR $wpdb->posts.post_status = 'pending'"; 
    21392166 
    21402167                        if ( is_user_logged_in() ) { 
    2141                                 $where .= current_user_can( "read_private_{$post_type_cap}s" ) ? " OR $wpdb->posts.post_status = 'private'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = 'private'"; 
     2168                                $where .= current_user_can( "read_private_{$post_type}s" ) ? " OR $wpdb->posts.post_status = 'private'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = 'private'"; 
    21422169                        } 
    21432170 
    21442171                        $where .= ')'; 
     
    22962323                                } else { 
    22972324                                        if  (in_array($status, array('draft', 'pending')) ) { 
    22982325                                                // User must have edit permissions on the draft to preview. 
    2299                                                 if (! current_user_can("edit_$post_type_cap", $this->posts[0]->ID)) { 
     2326                                                if (! current_user_can('edit_post', $this->posts[0]->ID)) { 
    23002327                                                        $this->posts = array(); 
    23012328                                                } else { 
    23022329                                                        $this->is_preview = true; 
     
    23042331                                                } 
    23052332                                        }  else if ('future' == $status) { 
    23062333                                                $this->is_preview = true; 
    2307                                                 if (!current_user_can("edit_$post_type_cap", $this->posts[0]->ID)) { 
     2334                                                if (!current_user_can('edit_post', $this->posts[0]->ID)) { 
    23082335                                                        $this->posts = array ( ); 
    23092336                                                } 
    23102337                                        } else { 
    2311                                                 if (! current_user_can("read_$post_type_cap", $this->posts[0]->ID)) 
     2338                                                if (! current_user_can('read_post', $this->posts[0]->ID)) 
    23122339                                                        $this->posts = array(); 
    23132340                                        } 
    23142341                                } 
    23152342                        } 
    23162343 
    2317                         if ( $this->is_preview && current_user_can( "edit_{$post_type_cap}", $this->posts[0]->ID ) ) 
     2344                        if ( $this->is_preview && current_user_can( "edit_{$post_type}", $this->posts[0]->ID ) ) 
    23182345                                $this->posts[0] = apply_filters('the_preview', $this->posts[0]); 
    23192346                } 
    23202347 
     
    27012728        } 
    27022729 
    27032730        do_action_ref_array('the_post', array(&$post)); 
    2704  
     2731         
    27052732        return true; 
    27062733} 
    27072734 
    2708 ?> 
     2735?> 
     2736 No newline at end of file