Make WordPress Core

Changeset 1354


Ignore:
Timestamp:
05/24/2004 02:55:39 AM (21 years ago)
Author:
rboren
Message:

Introducing query_posts(), update_post_caches(), update_user_cache(), and update_category_cache().

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-blog-header.php

    r1338 r1354  
    120120if (!isset($what_to_show))
    121121    $what_to_show = get_settings('what_to_show');
    122 $archive_mode = get_settings('archive_mode');
    123 $use_gzipcompression = get_settings('gzipcompression');
    124 
    125 // First let's clear some variables
    126 $whichcat = '';
    127 $whichauthor = '';
    128 $result = '';
    129 $where = '';
    130 $limits = '';
    131 $distinct = '';
    132 $join = '';
    133 
    134 if ($pagenow != 'post.php') { timer_start(); }
    135 
    136122if (isset($showposts) && $showposts) {
    137123    $showposts = (int)$showposts;
    138124    $posts_per_page = $showposts;
    139125}
     126$archive_mode = get_settings('archive_mode');
     127$use_gzipcompression = get_settings('gzipcompression');
    140128
    141 $add_hours = intval(get_settings('gmt_offset'));
    142 $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours));
    143 $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
     129$more_wpvars = array('posts_per_page', 'what_to_show', 'showposts');
    144130
    145 // If a month is specified in the querystring, load that month
    146 if ('' != $m) {
    147     $m = '' . preg_replace('|[^0-9]|', '', $m);
    148     $where .= ' AND YEAR(post_date)=' . substr($m, 0, 4);
    149     if (strlen($m)>5)
    150         $where .= ' AND MONTH(post_date)=' . substr($m, 4, 2);
    151     if (strlen($m)>7)
    152         $where .= ' AND DAYOFMONTH(post_date)=' . substr($m, 6, 2);
    153     if (strlen($m)>9)
    154         $where .= ' AND HOUR(post_date)=' . substr($m, 8, 2);
    155     if (strlen($m)>11)
    156         $where .= ' AND MINUTE(post_date)=' . substr($m, 10, 2);
    157     if (strlen($m)>13)
    158         $where .= ' AND SECOND(post_date)=' . substr($m, 12, 2);
    159 }
    160 
    161 if ('' != $hour) {
    162     $hour = '' . intval($hour);
    163     $where .= " AND HOUR(post_date)='$hour'";
    164 }
    165 
    166 if ('' != $minute) {
    167     $minute = '' . intval($minute);
    168     $where .= " AND MINUTE(post_date)='$minute'";
    169 }
    170 
    171 if ('' != $second) {
    172     $second = '' . intval($second);
    173     $where .= " AND SECOND(post_date)='$second'";
    174 }
    175 
    176 if ('' != $year) {
    177     $year = '' . intval($year);
    178     $where .= " AND YEAR(post_date)='$year'";
    179 }
    180 
    181 if ('' != $monthnum) {
    182     $monthnum = '' . intval($monthnum);
    183     $where .= " AND MONTH(post_date)='$monthnum'";
    184 }
    185 
    186 if ('' != $day) {
    187     $day = '' . intval($day);
    188     $where .= " AND DAYOFMONTH(post_date)='$day'";
    189 }
    190 
    191 if ('' != $name) {
    192     $name = preg_replace('/[^a-z0-9-_]/', '', $name);
    193     $where .= " AND post_name = '$name'";
    194 }
    195 
    196 if ('' != $w) {
    197     $w = ''.intval($w);
    198     $where .= " AND WEEK(post_date, 1)='$w'";
    199 }
    200 
    201 // If a post number is specified, load that post
    202 if (($p != '') && ($p != 'all')) {
    203     $p = intval($p);
    204     $where = ' AND ID = '.$p;
    205 }
    206 
    207 // If a search pattern is specified, load the posts that match
    208 if (!empty($s)) {
    209     $s = addslashes_gpc($s);
    210     $search = ' AND (';
    211     $s = preg_replace('/, +/', ' ', $s);
    212     $s = str_replace(',', ' ', $s);
    213     $s = str_replace('"', ' ', $s);
    214     $s = trim($s);
    215     if ($exact) {
    216         $n = '';
    217     } else {
    218         $n = '%';
    219     }
    220     if (!$sentence) {
    221         $s_array = explode(' ',$s);
    222         $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))';
    223         for ( $i = 1; $i < count($s_array); $i = $i + 1) {
    224             $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))';
    225         }
    226         $search .= ' OR (post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\')';
    227         $search .= ')';
    228     } else {
    229         $search = ' AND ((post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\'))';
     131// Construct the query string.
     132$query_string = '';
     133foreach (array_merge($wpvarstoreset, $more_wpvars) as $wpvar) {
     134    if ($$wpvar != '') {
     135        $query_string .= (strlen($query_string) < 1) ? '' : '&';
     136        $query_string .= $wpvar . '=' . rawurlencode($$wpvar);
    230137    }
    231138}
    232139
    233 // Category stuff
    234 $dogs = $wpdb->get_results("SELECT * FROM $tablecategories WHERE 1=1");
    235 foreach ($dogs as $catt) {
    236     $cache_categories[$catt->cat_ID] = $catt;
    237 }
     140if ($pagenow != 'post.php') { timer_start(); }
    238141
    239 if ((empty($cat)) || ($cat == 'all') || ($cat == '0') ||
    240     // Bypass cat checks if fetching specific posts
    241     (
    242         intval($year) || intval($monthnum) || intval($day) || intval($w) ||
    243         intval($p) || !empty($name) || !empty($s)
    244     )
    245         ) {
    246     $whichcat='';
    247 } else {
    248     $cat = ''.urldecode($cat).'';
    249     $cat = addslashes_gpc($cat);
    250     if (stristr($cat,'-')) {
    251         // Note: if we have a negative, we ignore all the positives. It must
    252         // always mean 'everything /except/ this one'. We should be able to do
    253         // multiple negatives but we don't :-(
    254         $eq = '!=';
    255         $andor = 'AND';
    256         $cat = explode('-',$cat);
    257         $cat = intval($cat[1]);
    258     } else {
    259         $eq = '=';
    260         $andor = 'OR';
     142// Update some caches.
     143update_user_cache();
     144update_category_cache();
     145
     146// Call query posts to do the work.
     147$posts = query_posts($query_string);
     148
     149// Update per post caches.
     150update_post_caches($posts);
     151
     152if (1 == count($posts)) {
     153    if ($p || $name) {
     154        $more = 1;
     155        $single = 1;
    261156    }
    262     $join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) ";
    263     $cat_array = explode(' ',$cat);
    264     $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]);
    265     $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');
    266     for ($i = 1; $i < (count($cat_array)); $i = $i + 1) {
    267             $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]);
    268         $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');
    269     }
    270     $whichcat .= ')';
    271     if ($eq == '!=') {
    272         $cat = '-'.$cat; // Put back the knowledge that we are excluding a category.
     157    if ($s && empty($paged)) { // If they were doing a search and got one result
     158        if (!strstr($_SERVER['PHP_SELF'], 'wp-admin')) // And not in admin section
     159            header('Location: ' . get_permalink($posts[0]->ID));
    273160    }
    274161}
    275162
    276 // Category stuff for nice URIs
    277 
    278 if ('' != $category_name) {
    279     if (stristr($category_name,'/')) {
    280         $category_name = explode('/',$category_name);
    281         if ($category_name[count($category_name)-1]) {
    282         $category_name = $category_name[count($category_name)-1]; // no trailing slash
    283         } else {
    284         $category_name = $category_name[count($category_name)-2]; // there was a trailling slash
    285         }
    286     }
    287     $category_name = preg_replace('|[^a-z0-9-_]|i', '', $category_name);
    288     $tables = ", $tablepost2cat, $tablecategories";
    289     $join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) LEFT JOIN $tablecategories ON ($tablepost2cat.category_id = $tablecategories.cat_ID) ";
    290     $whichcat = " AND (category_nicename = '$category_name'";
    291     $cat = $wpdb->get_var("SELECT cat_ID FROM $tablecategories WHERE category_nicename = '$category_name'");
    292     $whichcat .= get_category_children($cat, " OR category_id = ");
    293     $whichcat .= ")";
    294 }
    295 
    296 // Author/user stuff
    297 $users = $wpdb->get_results("SELECT * FROM $tableusers WHERE user_level > 0");
    298 foreach ($users as $user) {
    299     $cache_userdata[$user->ID] = $user;
    300 }
    301 
    302 if ((empty($author)) || ($author == 'all') || ($author == '0')) {
    303     $whichauthor='';
    304 } else {
    305     $author = ''.urldecode($author).'';
    306     $author = addslashes_gpc($author);
    307     if (stristr($author, '-')) {
    308         $eq = '!=';
    309         $andor = 'AND';
    310         $author = explode('-', $author);
    311         $author = ''.intval($author[1]);
    312     } else {
    313         $eq = '=';
    314         $andor = 'OR';
    315     }
    316     $author_array = explode(' ', $author);
    317     $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
    318     for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
    319         $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
    320     }
    321     $whichauthor .= ')';
    322 }
    323 
    324 // Author stuff for nice URIs
    325 
    326 if ('' != $author_name) {
    327     if (stristr($author_name,'/')) {
    328         $author_name = explode('/',$author_name);
    329         if ($author_name[count($author_name)-1]) {
    330         $author_name = $author_name[count($author_name)-1];#no trailing slash
    331         } else {
    332         $author_name = $author_name[count($author_name)-2];#there was a trailling slash
    333         }
    334     }
    335     $author_name = preg_replace('|[^a-z0-9-_]|', '', strtolower($author_name));
    336     $author = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_nicename='".$author_name."'");
    337     $whichauthor .= ' AND (post_author = '.intval($author).')';
    338 }
    339 
    340 $where .= $search.$whichcat.$whichauthor;
    341 
    342 if ((empty($order)) || ((strtoupper($order) != 'ASC') && (strtoupper($order) != 'DESC'))) {
    343     $order='DESC';
    344 }
    345 
    346 // Order by
    347 if (empty($orderby)) {
    348     $orderby='date '.$order;
    349 } else {
    350     // Used to filter values
    351     $allowed_keys = array('author','date','category','title');
    352     $orderby = urldecode($orderby);
    353     $orderby = addslashes_gpc($orderby);
    354     $orderby_array = explode(' ',$orderby);
    355     if (!in_array($orderby_array[0],$allowed_keys)) {
    356         $orderby_array[0] = 'date';
    357     }
    358     $orderby = $orderby_array[0].' '.$order;
    359     if (count($orderby_array)>1) {
    360         for ($i = 1; $i < (count($orderby_array)); $i = $i + 1) {
    361             // Only allow certain values for safety
    362             if (in_array($orderby_array[$i],$allowed_keys)) {
    363                 $orderby .= ',post_'.$orderby_array[$i].' '.$order;
    364             }
    365         }
    366     }
    367 }
    368 
    369 if ((!$whichcat) && (!$m) && (!$p) && (!$w) && (!$s) && empty($poststart) && empty($postend)) {
    370     if ($what_to_show == 'posts') {
    371         $limits = ' LIMIT '.$posts_per_page;
    372     } elseif ($what_to_show == 'days' && empty($monthnum) && empty($year) && empty($day)) {
    373         $lastpostdate = get_lastpostdate();
    374         $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate);
    375         $lastpostdate = mysql2date('U',$lastpostdate);
    376         $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($posts_per_page-1) * 86400)));
    377         $where .= " AND post_date > '$otherdate'";
    378     }
    379 }
    380 
    381 if ( !empty($postend) && ($postend > $poststart) && (!$m) && empty($monthnum) && empty($year) && empty($day) &&(!$w) && (!$whichcat) && (!$s) && (!$p)) {
    382     if ($what_to_show == 'posts' || ($what_to_show == 'paged' && (!$paged))) {
    383         $poststart = intval($poststart);
    384         $postend = intval($postend);
    385         $limposts = $postend - $poststart;
    386         $limits = ' LIMIT '.$poststart.','.$limposts;
    387     } elseif ($what_to_show == 'days') {
    388         $poststart = intval($poststart);
    389         $postend = intval($postend);
    390         $limposts = $postend - $poststart;
    391         $lastpostdate = get_lastpostdate();
    392         $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate);
    393         $lastpostdate = mysql2date('U',$lastpostdate);
    394         $startdate = date('Y-m-d H:i:s', ($lastpostdate - (($poststart -1) * 86400)));
    395         $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($postend -1) * 86400)));
    396         $where .= " AND post_date > '$otherdate' AND post_date < '$startdate'";
    397     }
    398 } else {
    399     if (($what_to_show == 'paged') && (!$p) && (!$more)) {
    400         if ($pagenow != 'post.php') {
    401             $pgstrt = '';
    402             if ($paged) {
    403                 $pgstrt = (intval($paged) -1) * $posts_per_page . ', ';
    404             }
    405             $limits = 'LIMIT '.$pgstrt.$posts_per_page;
    406         } else {
    407             if (($m) || ($p) || ($w) || ($s) || ($whichcat)) {
    408                 $limits = '';
    409             } else {
    410                 $pgstrt = '';
    411                 if ($paged) {
    412                     $pgstrt = (intval($paged) -1) * $posts_per_page . ', ';
    413                 }
    414                 $limits = 'LIMIT '.$pgstrt.$posts_per_page;
    415             }
    416         }
    417     }
    418     elseif (($m) || ($p) || ($w) || ($s) || ($whichcat) || ($author) || $monthnum || $year || $day) {
    419         $limits = '';
    420     }
    421 }
    422 
    423 if ($p == 'all') {
    424     $where = '';
    425 }
    426 
    427 $now = gmdate('Y-m-d H:i:59');
    428 
    429163if ($pagenow != 'post.php' && $pagenow != 'edit.php') {
    430     if ((empty($poststart)) || (empty($postend)) || !($postend > $poststart)) {
    431         $where .= " AND post_date_gmt <= '$now'";
    432     }
    433 
    434     $distinct = 'DISTINCT';
    435 
    436164    if ($use_gzipcompression) {
    437165        // gzipping the output of the script
     
    439167    }
    440168}
    441 $where .= ' AND (post_status = "publish"';
    442 
    443 // Get private posts
    444 if (isset($user_ID) && ('' != intval($user_ID)))
    445     $where .= " OR post_author = $user_ID AND post_status != 'draft')";
    446 else
    447     $where .= ')';
    448 $where .= " GROUP BY $tableposts.ID";
    449 $request = " SELECT $distinct * FROM $tableposts $join WHERE 1=1".$where." ORDER BY post_$orderby $limits";
    450 
    451 
    452 if ($preview) {
    453     $request = 'SELECT 1-1'; // dummy mysql query for the preview
    454     // little funky fix for IEwin, rawk on that code
    455     $is_winIE = ((preg_match('/MSIE/',$HTTP_USER_AGENT)) && (preg_match('/Win/',$HTTP_USER_AGENT)));
    456     if (($is_winIE) && (!isset($IEWin_bookmarklet_fix))) {
    457         $preview_content =  preg_replace('/\%u([0-9A-F]{4,4})/e',  "'&#'.base_convert('\\1',16,10).';'", $preview_content);
    458     }
    459 }
    460 
    461 // error_log("$request");
    462 // echo $request;
    463 $posts = $wpdb->get_results($request);
    464 
    465 // No point in doing all this work if we didn't match any posts.
    466 if ($posts) {
    467     // Get the categories for all the posts
    468     foreach ($posts as $post) {
    469         $post_id_list[] = $post->ID;
    470     }
    471     $post_id_list = implode(',', $post_id_list);
    472 
    473     $dogs = $wpdb->get_results("SELECT DISTINCT
    474         ID, category_id, cat_name, category_nicename, category_description, category_parent
    475         FROM $tablecategories, $tablepost2cat, $tableposts
    476         WHERE category_id = cat_ID AND post_id = ID AND post_id IN ($post_id_list)");
    477        
    478     foreach ($dogs as $catt) {
    479         $category_cache[$catt->ID][] = $catt;
    480     }
    481 
    482     // Do the same for comment numbers
    483     $comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
    484         FROM $tableposts
    485         LEFT JOIN $tablecomments ON ( comment_post_ID = ID  AND comment_approved =  '1')
    486         WHERE post_status =  'publish' AND ID IN ($post_id_list)
    487         GROUP BY ID");
    488    
    489     if ($comment_counts) {
    490         foreach ($comment_counts as $comment_count) {
    491             $comment_count_cache["$comment_count->ID"] = $comment_count->ccount;
    492         }
    493     }
    494 
    495     // Get post-meta info
    496     if ( $meta_list = $wpdb->get_results("
    497             SELECT post_id,meta_key,meta_value
    498             FROM $tablepostmeta
    499             WHERE post_id IN($post_id_list)
    500             ORDER BY post_id,meta_key
    501         ", ARRAY_A) ) {
    502        
    503         // Change from flat structure to hierarchical:
    504         $post_meta_cache = array();
    505         foreach ($meta_list as $metarow) {
    506             $mpid = $metarow['post_id'];
    507             $mkey = $metarow['meta_key'];
    508             $mval = $metarow['meta_value'];
    509            
    510             // Force subkeys to be array type:
    511             if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]))
    512                 $post_meta_cache[$mpid] = array();
    513             if (!isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]))
    514                 $post_meta_cache[$mpid]["$mkey"] = array();
    515            
    516             // Add a value to the current pid/key:
    517             $post_meta_cache[$mpid][$mkey][] = $mval;
    518         }
    519     }
    520 
    521 
    522     if (1 == count($posts)) {
    523         if ($p || $name) {
    524             $more = 1;
    525             $single = 1;
    526         }
    527         if ($s && empty($paged)) { // If they were doing a search and got one result
    528             if (!strstr($_SERVER['PHP_SELF'], 'wp-admin')) // And not in admin section
    529                 header('Location: ' . get_permalink($posts[0]->ID));
    530         }
    531     }
    532 } // End if posts.
    533169?>
  • trunk/wp-includes/functions.php

    r1347 r1354  
    13541354}
    13551355
     1356function query_posts($query) {
     1357    global $wpdb, $tablepost2cat, $tableposts, $tablecategories, $tableusers,
     1358        $pagenow;
     1359
     1360    parse_str($query);
     1361
     1362    // First let's clear some variables
     1363    $whichcat = '';
     1364    $whichauthor = '';
     1365    $result = '';
     1366    $where = '';
     1367    $limits = '';
     1368    $distinct = '';
     1369    $join = '';
     1370
     1371    $add_hours = intval(get_settings('gmt_offset'));
     1372    $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours));
     1373    $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
     1374
     1375    // If a month is specified in the querystring, load that month
     1376    if ('' != $m) {
     1377        $m = '' . preg_replace('|[^0-9]|', '', $m);
     1378        $where .= ' AND YEAR(post_date)=' . substr($m, 0, 4);
     1379        if (strlen($m)>5)
     1380            $where .= ' AND MONTH(post_date)=' . substr($m, 4, 2);
     1381        if (strlen($m)>7)
     1382            $where .= ' AND DAYOFMONTH(post_date)=' . substr($m, 6, 2);
     1383        if (strlen($m)>9)
     1384            $where .= ' AND HOUR(post_date)=' . substr($m, 8, 2);
     1385        if (strlen($m)>11)
     1386            $where .= ' AND MINUTE(post_date)=' . substr($m, 10, 2);
     1387        if (strlen($m)>13)
     1388            $where .= ' AND SECOND(post_date)=' . substr($m, 12, 2);
     1389    }
     1390
     1391    if ('' != $hour) {
     1392        $hour = '' . intval($hour);
     1393        $where .= " AND HOUR(post_date)='$hour'";
     1394    }
     1395
     1396    if ('' != $minute) {
     1397        $minute = '' . intval($minute);
     1398        $where .= " AND MINUTE(post_date)='$minute'";
     1399    }
     1400
     1401    if ('' != $second) {
     1402        $second = '' . intval($second);
     1403        $where .= " AND SECOND(post_date)='$second'";
     1404    }
     1405
     1406    if ('' != $year) {
     1407        $year = '' . intval($year);
     1408        $where .= " AND YEAR(post_date)='$year'";
     1409    }
     1410
     1411    if ('' != $monthnum) {
     1412        $monthnum = '' . intval($monthnum);
     1413        $where .= " AND MONTH(post_date)='$monthnum'";
     1414    }
     1415
     1416    if ('' != $day) {
     1417        $day = '' . intval($day);
     1418        $where .= " AND DAYOFMONTH(post_date)='$day'";
     1419    }
     1420
     1421    if ('' != $name) {
     1422        $name = preg_replace('/[^a-z0-9-_]/', '', $name);
     1423        $where .= " AND post_name = '$name'";
     1424    }
     1425
     1426    if ('' != $w) {
     1427        $w = ''.intval($w);
     1428        $where .= " AND WEEK(post_date, 1)='$w'";
     1429    }
     1430
     1431    // If a post number is specified, load that post
     1432    if (($p != '') && ($p != 'all')) {
     1433        $p = intval($p);
     1434        $where = ' AND ID = '.$p;
     1435    }
     1436
     1437    // If a search pattern is specified, load the posts that match
     1438    if (!empty($s)) {
     1439        $s = addslashes_gpc($s);
     1440        $search = ' AND (';
     1441        $s = preg_replace('/, +/', ' ', $s);
     1442        $s = str_replace(',', ' ', $s);
     1443        $s = str_replace('"', ' ', $s);
     1444        $s = trim($s);
     1445        if ($exact) {
     1446            $n = '';
     1447        } else {
     1448            $n = '%';
     1449        }
     1450        if (!$sentence) {
     1451            $s_array = explode(' ',$s);
     1452            $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))';
     1453            for ( $i = 1; $i < count($s_array); $i = $i + 1) {
     1454                $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))';
     1455            }
     1456            $search .= ' OR (post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\')';
     1457            $search .= ')';
     1458        } else {
     1459            $search = ' AND ((post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\'))';
     1460        }
     1461    }
     1462
     1463    // Category stuff
     1464
     1465    if ((empty($cat)) || ($cat == 'all') || ($cat == '0') ||
     1466        // Bypass cat checks if fetching specific posts
     1467        (
     1468         intval($year) || intval($monthnum) || intval($day) || intval($w) ||
     1469         intval($p) || !empty($name) || !empty($s)
     1470         )
     1471        ) {
     1472        $whichcat='';
     1473    } else {
     1474        $cat = ''.urldecode($cat).'';
     1475        $cat = addslashes_gpc($cat);
     1476        if (stristr($cat,'-')) {
     1477            // Note: if we have a negative, we ignore all the positives. It must
     1478            // always mean 'everything /except/ this one'. We should be able to do
     1479            // multiple negatives but we don't :-(
     1480            $eq = '!=';
     1481            $andor = 'AND';
     1482            $cat = explode('-',$cat);
     1483            $cat = intval($cat[1]);
     1484        } else {
     1485            $eq = '=';
     1486            $andor = 'OR';
     1487        }
     1488        $join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) ";
     1489        $cat_array = explode(' ',$cat);
     1490        $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]);
     1491        $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');
     1492        for ($i = 1; $i < (count($cat_array)); $i = $i + 1) {
     1493            $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]);
     1494            $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');
     1495        }
     1496        $whichcat .= ')';
     1497        if ($eq == '!=') {
     1498            $cat = '-'.$cat; // Put back the knowledge that we are excluding a category.
     1499        }
     1500    }
     1501
     1502    // Category stuff for nice URIs
     1503
     1504    if ('' != $category_name) {
     1505        if (stristr($category_name,'/')) {
     1506            $category_name = explode('/',$category_name);
     1507            if ($category_name[count($category_name)-1]) {
     1508                $category_name = $category_name[count($category_name)-1]; // no trailing slash
     1509            } else {
     1510                $category_name = $category_name[count($category_name)-2]; // there was a trailling slash
     1511            }
     1512        }
     1513        $category_name = preg_replace('|[^a-z0-9-_]|i', '', $category_name);
     1514        $tables = ", $tablepost2cat, $tablecategories";
     1515        $join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) LEFT JOIN $tablecategories ON ($tablepost2cat.category_id = $tablecategories.cat_ID) ";
     1516        $whichcat = " AND (category_nicename = '$category_name'";
     1517        $cat = $wpdb->get_var("SELECT cat_ID FROM $tablecategories WHERE category_nicename = '$category_name'");
     1518        $whichcat .= get_category_children($cat, " OR category_id = ");
     1519        $whichcat .= ")";
     1520    }
     1521
     1522    // Author/user stuff
     1523
     1524    if ((empty($author)) || ($author == 'all') || ($author == '0')) {
     1525        $whichauthor='';
     1526    } else {
     1527        $author = ''.urldecode($author).'';
     1528        $author = addslashes_gpc($author);
     1529        if (stristr($author, '-')) {
     1530            $eq = '!=';
     1531            $andor = 'AND';
     1532            $author = explode('-', $author);
     1533            $author = ''.intval($author[1]);
     1534        } else {
     1535            $eq = '=';
     1536            $andor = 'OR';
     1537        }
     1538        $author_array = explode(' ', $author);
     1539        $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
     1540        for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
     1541            $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
     1542        }
     1543        $whichauthor .= ')';
     1544    }
     1545
     1546    // Author stuff for nice URIs
     1547
     1548    if ('' != $author_name) {
     1549        if (stristr($author_name,'/')) {
     1550            $author_name = explode('/',$author_name);
     1551            if ($author_name[count($author_name)-1]) {
     1552                $author_name = $author_name[count($author_name)-1];#no trailing slash
     1553            } else {
     1554                $author_name = $author_name[count($author_name)-2];#there was a trailling slash
     1555            }
     1556        }
     1557        $author_name = preg_replace('|[^a-z0-9-_]|', '', strtolower($author_name));
     1558        $author = $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_nicename='".$author_name."'");
     1559        $whichauthor .= ' AND (post_author = '.intval($author).')';
     1560    }
     1561
     1562    $where .= $search.$whichcat.$whichauthor;
     1563
     1564    if ((empty($order)) || ((strtoupper($order) != 'ASC') && (strtoupper($order) != 'DESC'))) {
     1565        $order='DESC';
     1566    }
     1567
     1568    // Order by
     1569    if (empty($orderby)) {
     1570        $orderby='date '.$order;
     1571    } else {
     1572        // Used to filter values
     1573        $allowed_keys = array('author','date','category','title');
     1574        $orderby = urldecode($orderby);
     1575        $orderby = addslashes_gpc($orderby);
     1576        $orderby_array = explode(' ',$orderby);
     1577        if (!in_array($orderby_array[0],$allowed_keys)) {
     1578            $orderby_array[0] = 'date';
     1579        }
     1580        $orderby = $orderby_array[0].' '.$order;
     1581        if (count($orderby_array)>1) {
     1582            for ($i = 1; $i < (count($orderby_array)); $i = $i + 1) {
     1583                // Only allow certain values for safety
     1584                if (in_array($orderby_array[$i],$allowed_keys)) {
     1585                    $orderby .= ',post_'.$orderby_array[$i].' '.$order;
     1586                }
     1587            }
     1588        }
     1589    }
     1590
     1591    if ((!$whichcat) && (!$m) && (!$p) && (!$w) && (!$s) && empty($poststart) && empty($postend)) {
     1592        if ($what_to_show == 'posts') {
     1593            $limits = ' LIMIT '.$posts_per_page;
     1594        } elseif ($what_to_show == 'days' && empty($monthnum) && empty($year) && empty($day)) {
     1595            $lastpostdate = get_lastpostdate();
     1596            $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate);
     1597            $lastpostdate = mysql2date('U',$lastpostdate);
     1598            $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($posts_per_page-1) * 86400)));
     1599            $where .= " AND post_date > '$otherdate'";
     1600        }
     1601    }
     1602
     1603    if ( !empty($postend) && ($postend > $poststart) && (!$m) && empty($monthnum) && empty($year) && empty($day) &&(!$w) && (!$whichcat) && (!$s) && (!$p)) {
     1604        if ($what_to_show == 'posts' || ($what_to_show == 'paged' && (!$paged))) {
     1605            $poststart = intval($poststart);
     1606            $postend = intval($postend);
     1607            $limposts = $postend - $poststart;
     1608            $limits = ' LIMIT '.$poststart.','.$limposts;
     1609        } elseif ($what_to_show == 'days') {
     1610            $poststart = intval($poststart);
     1611            $postend = intval($postend);
     1612            $limposts = $postend - $poststart;
     1613            $lastpostdate = get_lastpostdate();
     1614            $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate);
     1615            $lastpostdate = mysql2date('U',$lastpostdate);
     1616            $startdate = date('Y-m-d H:i:s', ($lastpostdate - (($poststart -1) * 86400)));
     1617            $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($postend -1) * 86400)));
     1618            $where .= " AND post_date > '$otherdate' AND post_date < '$startdate'";
     1619        }
     1620    } else {
     1621        if (($what_to_show == 'paged') && (!$p) && (!$more)) {
     1622            if ($pagenow != 'post.php') {
     1623                $pgstrt = '';
     1624                if ($paged) {
     1625                    $pgstrt = (intval($paged) -1) * $posts_per_page . ', ';
     1626                }
     1627                $limits = 'LIMIT '.$pgstrt.$posts_per_page;
     1628            } else {
     1629                if (($m) || ($p) || ($w) || ($s) || ($whichcat)) {
     1630                    $limits = '';
     1631                } else {
     1632                    $pgstrt = '';
     1633                    if ($paged) {
     1634                        $pgstrt = (intval($paged) -1) * $posts_per_page . ', ';
     1635                    }
     1636                    $limits = 'LIMIT '.$pgstrt.$posts_per_page;
     1637                }
     1638            }
     1639        }
     1640        elseif (($m) || ($p) || ($w) || ($s) || ($whichcat) || ($author) || $monthnum || $year || $day) {
     1641            $limits = '';
     1642        }
     1643    }
     1644
     1645    if ($p == 'all') {
     1646        $where = '';
     1647    }
     1648
     1649    $now = gmdate('Y-m-d H:i:59');
     1650
     1651    if ($pagenow != 'post.php' && $pagenow != 'edit.php') {
     1652        if ((empty($poststart)) || (empty($postend)) || !($postend > $poststart)) {
     1653            $where .= " AND post_date_gmt <= '$now'";
     1654        }
     1655
     1656        $distinct = 'DISTINCT';
     1657    }
     1658    $where .= ' AND (post_status = "publish"';
     1659
     1660    // Get private posts
     1661    if (isset($user_ID) && ('' != intval($user_ID)))
     1662        $where .= " OR post_author = $user_ID AND post_status != 'draft')";
     1663    else
     1664        $where .= ')';
     1665    $where .= " GROUP BY $tableposts.ID";
     1666    $request = " SELECT $distinct * FROM $tableposts $join WHERE 1=1".$where." ORDER BY post_$orderby $limits";
     1667
     1668
     1669    if ($preview) {
     1670        $request = 'SELECT 1-1'; // dummy mysql query for the preview
     1671        // little funky fix for IEwin, rawk on that code
     1672        $is_winIE = ((preg_match('/MSIE/',$HTTP_USER_AGENT)) && (preg_match('/Win/',$HTTP_USER_AGENT)));
     1673        if (($is_winIE) && (!isset($IEWin_bookmarklet_fix))) {
     1674            $preview_content =  preg_replace('/\%u([0-9A-F]{4,4})/e',  "'&#'.base_convert('\\1',16,10).';'", $preview_content);
     1675        }
     1676    }
     1677
     1678    // error_log("$request");
     1679    // echo $request;
     1680    return $wpdb->get_results($request);
     1681}
     1682
     1683function update_post_caches($posts) {
     1684    global $category_cache, $comment_count_cache, $post_meta_cache;
     1685    global $tablecategories, $tablepost2cat, $tableposts, $tablecomments,
     1686        $tablepostmeta, $wpdb;
     1687
     1688    // No point in doing all this work if we didn't match any posts.
     1689    if (! $posts) {
     1690        return;
     1691    }
     1692
     1693    // Get the categories for all the posts
     1694    foreach ($posts as $post) {
     1695        $post_id_list[] = $post->ID;
     1696    }
     1697    $post_id_list = implode(',', $post_id_list);
     1698
     1699    $dogs = $wpdb->get_results("SELECT DISTINCT
     1700        ID, category_id, cat_name, category_nicename, category_description, category_parent
     1701        FROM $tablecategories, $tablepost2cat, $tableposts
     1702        WHERE category_id = cat_ID AND post_id = ID AND post_id IN ($post_id_list)");
     1703       
     1704    foreach ($dogs as $catt) {
     1705        $category_cache[$catt->ID][] = $catt;
     1706    }
     1707
     1708    // Do the same for comment numbers
     1709    $comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
     1710        FROM $tableposts
     1711        LEFT JOIN $tablecomments ON ( comment_post_ID = ID  AND comment_approved =  '1')
     1712        WHERE post_status =  'publish' AND ID IN ($post_id_list)
     1713        GROUP BY ID");
     1714   
     1715    if ($comment_counts) {
     1716        foreach ($comment_counts as $comment_count) {
     1717            $comment_count_cache["$comment_count->ID"] = $comment_count->ccount;
     1718        }
     1719    }
     1720
     1721    // Get post-meta info
     1722    if ( $meta_list = $wpdb->get_results("
     1723            SELECT post_id,meta_key,meta_value
     1724            FROM $tablepostmeta
     1725            WHERE post_id IN($post_id_list)
     1726            ORDER BY post_id,meta_key
     1727        ", ARRAY_A) ) {
     1728       
     1729        // Change from flat structure to hierarchical:
     1730        $post_meta_cache = array();
     1731        foreach ($meta_list as $metarow) {
     1732            $mpid = $metarow['post_id'];
     1733            $mkey = $metarow['meta_key'];
     1734            $mval = $metarow['meta_value'];
     1735           
     1736            // Force subkeys to be array type:
     1737            if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]))
     1738                $post_meta_cache[$mpid] = array();
     1739            if (!isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]))
     1740                $post_meta_cache[$mpid]["$mkey"] = array();
     1741           
     1742            // Add a value to the current pid/key:
     1743            $post_meta_cache[$mpid][$mkey][] = $mval;
     1744        }
     1745    }
     1746}
     1747
     1748function update_category_cache() {
     1749    global $cache_categories, $tablecategories, $wpdb;
     1750    $dogs = $wpdb->get_results("SELECT * FROM $tablecategories WHERE 1=1");
     1751    foreach ($dogs as $catt) {
     1752        $cache_categories[$catt->cat_ID] = $catt;
     1753    }
     1754}
     1755
     1756function update_user_cache() {
     1757    global $cache_userdata, $tableusers, $wpdb;
     1758
     1759    $users = $wpdb->get_results("SELECT * FROM $tableusers WHERE user_level > 0");
     1760    foreach ($users as $user) {
     1761        $cache_userdata[$user->ID] = $user;
     1762    }
     1763}
     1764
    13561765function wp_head() {
    13571766    do_action('wp_head', '');
Note: See TracChangeset for help on using the changeset viewer.