Ticket #9951: query.2.diff
File query.2.diff, 8.6 KB (added by , 16 years ago) |
---|
-
query.php
1396 1396 $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']); 1397 1397 $this->is_tag = true; 1398 1398 } 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 } 1399 1418 1400 1419 if ( empty($qv['taxonomy']) || empty($qv['term']) ) { 1401 1420 $this->is_tax = false; … … 1596 1615 if ( $this->is_search ) 1597 1616 $q['post_type'] = 'any'; 1598 1617 else 1599 $q['post_type'] = ' ';1618 $q['post_type'] = 'post'; 1600 1619 } 1601 1620 $post_type = $q['post_type']; 1602 1621 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 ) … … 1756 1775 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))"; 1757 1776 $searchand = ' AND '; 1758 1777 } 1759 $term = esc_sql($q['s']);1778 $term = $wpdb->escape($q['s']); 1760 1779 if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] ) 1761 1780 $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')"; 1762 1781 … … 1902 1921 $whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')"; 1903 1922 } 1904 1923 } 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 } 1905 1942 1906 // Tag and slug intersections. 1943 // Tag and slug intersections. Also merge with custom taxonomies 1907 1944 $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 ); 1908 1946 $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below 1909 1947 foreach ($intersections as $item => $taxonomy) { 1910 1948 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 1913 1950 if ( $item != 'category__and' ) { 1914 1951 $reqtag = is_term( $q[$item][0], 'post_tag' ); 1915 1952 if ( !empty($reqtag) ) 1916 1953 $q['tag_id'] = $reqtag['term_id']; 1917 1954 } 1918 1955 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'; 1923 1957 1924 1958 $q[$item] = array_unique($q[$item]); 1925 1959 $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]) . "')"; 1927 1961 if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship 1928 1962 $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]); 1929 1963 } … … 2070 2104 $q['orderby'] = "$wpdb->posts.post_date ".$q['order']; 2071 2105 } 2072 2106 2073 $post_type_cap = $post_type;2074 2075 2107 if ( 'any' == $post_type ) { 2076 2108 $where .= " AND $wpdb->posts.post_type != 'revision'"; 2077 } elseif ( ! empty( $post_type ) ) {2078 $where .= " AND $wpdb->posts.post_type = '$post_type'";2079 2109 } elseif ( $this->is_attachment ) { 2080 2110 $where .= " AND $wpdb->posts.post_type = 'attachment'"; 2081 $post_type_cap = 'post';2082 2111 } elseif ($this->is_page) { 2083 2112 $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'"; 2085 2115 } else { 2086 $where .= " AND $wpdb->posts.post_type = 'post'"; 2087 $post_type_cap = 'post'; 2116 $where .= " AND $wpdb->posts.post_type = '$post_type'"; 2088 2117 } 2089 2118 2090 2119 if ( isset($q['post_status']) && '' != $q['post_status'] ) { … … 2104 2133 $p_status[] = "$wpdb->posts.post_status = 'private'"; 2105 2134 if ( in_array( 'publish', $q_status ) ) 2106 2135 $r_status[] = "$wpdb->posts.post_status = 'publish'"; 2107 if ( in_array( 'trash', $q_status ) )2108 $r_status[] = "$wpdb->posts.post_status = 'trash'";2109 2136 2110 2137 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) { 2111 2138 $r_status = array_merge($r_status, $p_status); … … 2113 2140 } 2114 2141 2115 2142 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") ) 2117 2144 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))"; 2118 2145 else 2119 2146 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")"; 2120 2147 } 2121 2148 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") ) 2123 2150 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))"; 2124 2151 else 2125 2152 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")"; … … 2138 2165 $where .= " OR $wpdb->posts.post_status = 'future' OR $wpdb->posts.post_status = 'draft' OR $wpdb->posts.post_status = 'pending'"; 2139 2166 2140 2167 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'"; 2142 2169 } 2143 2170 2144 2171 $where .= ')'; … … 2296 2323 } else { 2297 2324 if (in_array($status, array('draft', 'pending')) ) { 2298 2325 // 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)) { 2300 2327 $this->posts = array(); 2301 2328 } else { 2302 2329 $this->is_preview = true; … … 2304 2331 } 2305 2332 } else if ('future' == $status) { 2306 2333 $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)) { 2308 2335 $this->posts = array ( ); 2309 2336 } 2310 2337 } 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)) 2312 2339 $this->posts = array(); 2313 2340 } 2314 2341 } 2315 2342 } 2316 2343 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 ) ) 2318 2345 $this->posts[0] = apply_filters('the_preview', $this->posts[0]); 2319 2346 } 2320 2347 … … 2701 2728 } 2702 2729 2703 2730 do_action_ref_array('the_post', array(&$post)); 2704 2731 2705 2732 return true; 2706 2733 } 2707 2734 2708 ?> 2735 ?> 2736 No newline at end of file