WordPress.org

Make WordPress Core

Changeset 4426


Ignore:
Timestamp:
10/27/06 03:47:43 (9 years ago)
Author:
markjaquith
Message:

Allow "quoted strings" to be used in searches. mega free-beer-when-I-finally-meet-you-in-person props to ringmaster. fixes #3177

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/query.php

    r4414 r4426  
    744744        // If a search pattern is specified, load the posts that match 
    745745        if (!empty($q['s'])) { 
    746             $q['s'] = addslashes_gpc($q['s']); 
    747             $search = ' AND ('; 
    748             $q['s'] = preg_replace('/, +/', ' ', $q['s']); 
    749             $q['s'] = str_replace(',', ' ', $q['s']); 
    750             $q['s'] = str_replace('"', ' ', $q['s']); 
    751             $q['s'] = trim($q['s']); 
    752             if ($q['exact']) { 
    753                 $n = ''; 
    754             } else { 
    755                 $n = '%'; 
    756             } 
    757             if (!$q['sentence']) { 
    758                 $s_array = explode(' ',$q['s']); 
    759                 $q['search_terms'] = $s_array; 
    760                 $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))'; 
    761                 for ( $i = 1; $i < count($s_array); $i = $i + 1) { 
    762                     $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))'; 
    763                 } 
    764                 $search .= ' OR (post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\')'; 
    765                 $search .= ')'; 
    766             } else { 
    767                 $search = ' AND ((post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\'))'; 
    768             } 
     746            // added slashes screw with quote grouping when done early, so done later 
     747            $q['s'] = stripslashes($q['s']); 
     748            if ($q['sentence']) { 
     749                $q['search_terms'] = array($q['s']); 
     750            } 
     751            else { 
     752                preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q[s], $matches); 
     753                $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]); 
     754            } 
     755            $n = ($q['exact']) ? '' : '%'; 
     756            $searchand = ''; 
     757            foreach((array)$q['search_terms'] as $term) { 
     758                $term = addslashes_gpc($term); 
     759                $search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}'))"; 
     760                $searchand = ' AND '; 
     761            } 
     762            $term = addslashes_gpc($q['s']);  
     763            if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] ) $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}')"; 
     764             
     765            $search = " AND ({$search}) "; 
    769766        } 
    770767 
Note: See TracChangeset for help on using the changeset viewer.