Make WordPress Core


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().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.