Make WordPress Core


Ignore:
Timestamp:
08/08/2004 03:31:50 PM (21 years ago)
Author:
rboren
Message:

WP Query class.

File:
1 edited

Legend:

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

    r1513 r1516  
    10371037
    10381038
    1039 function start_wp() {
    1040     global $post, $id, $postdata, $authordata, $day, $preview, $page, $pages, $multipage, $more, $numpages;
     1039function start_wp($use_wp_query = false) {
     1040  global $post, $id, $postdata, $authordata, $day, $preview, $page, $pages, $multipage, $more, $numpages, $wp_query;
    10411041    global $pagenow;
     1042
     1043    if ($use_wp_query) {
     1044      $post = $wp_query->next_post();
     1045    } else {
     1046      $wp_query->next_post();
     1047    }
     1048
    10421049    if (!$preview) {
    10431050        $id = $post->ID;
     
    14321439
    14331440function query_posts($query) {
    1434     global $wpdb, $pagenow, $request, $user_ID;
    1435 
    1436     parse_str($query);
    1437 
    1438     // First let's clear some variables
    1439     $whichcat = '';
    1440     $whichauthor = '';
    1441     $result = '';
    1442     $where = '';
    1443     $limits = '';
    1444     $distinct = '';
    1445     $join = '';
    1446 
    1447     $add_hours = intval(get_settings('gmt_offset'));
    1448     $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours));
    1449     $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
    1450 
    1451     // If a month is specified in the querystring, load that month
    1452     if ('' != $m) {
    1453         $m = '' . preg_replace('|[^0-9]|', '', $m);
    1454         $where .= ' AND YEAR(post_date)=' . substr($m, 0, 4);
    1455         if (strlen($m)>5)
    1456             $where .= ' AND MONTH(post_date)=' . substr($m, 4, 2);
    1457         if (strlen($m)>7)
    1458             $where .= ' AND DAYOFMONTH(post_date)=' . substr($m, 6, 2);
    1459         if (strlen($m)>9)
    1460             $where .= ' AND HOUR(post_date)=' . substr($m, 8, 2);
    1461         if (strlen($m)>11)
    1462             $where .= ' AND MINUTE(post_date)=' . substr($m, 10, 2);
    1463         if (strlen($m)>13)
    1464             $where .= ' AND SECOND(post_date)=' . substr($m, 12, 2);
    1465     }
    1466 
    1467     if ('' != $hour) {
    1468         $hour = '' . intval($hour);
    1469         $where .= " AND HOUR(post_date)='$hour'";
    1470     }
    1471 
    1472     if ('' != $minute) {
    1473         $minute = '' . intval($minute);
    1474         $where .= " AND MINUTE(post_date)='$minute'";
    1475     }
    1476 
    1477     if ('' != $second) {
    1478         $second = '' . intval($second);
    1479         $where .= " AND SECOND(post_date)='$second'";
    1480     }
    1481 
    1482     if ('' != $year) {
    1483         $year = '' . intval($year);
    1484         $where .= " AND YEAR(post_date)='$year'";
    1485     }
    1486 
    1487     if ('' != $monthnum) {
    1488         $monthnum = '' . intval($monthnum);
    1489         $where .= " AND MONTH(post_date)='$monthnum'";
    1490     }
    1491 
    1492     if ('' != $day) {
    1493         $day = '' . intval($day);
    1494         $where .= " AND DAYOFMONTH(post_date)='$day'";
    1495     }
    1496 
    1497     if ('' != $name) {
    1498         $name = preg_replace('/[^a-z0-9-_]/', '', $name);
    1499         $where .= " AND post_name = '$name'";
    1500     }
    1501 
    1502     if ('' != $w) {
    1503         $w = ''.intval($w);
    1504         $where .= " AND WEEK(post_date, 1)='$w'";
    1505     }
    1506 
    1507     // If a post number is specified, load that post
    1508     if (($p != '') && ($p != 'all')) {
    1509         $p = intval($p);
    1510         $where = ' AND ID = '.$p;
    1511     }
    1512 
    1513     // If a search pattern is specified, load the posts that match
    1514     if (!empty($s)) {
    1515         $s = addslashes_gpc($s);
    1516         $search = ' AND (';
    1517         $s = preg_replace('/, +/', ' ', $s);
    1518         $s = str_replace(',', ' ', $s);
    1519         $s = str_replace('"', ' ', $s);
    1520         $s = trim($s);
    1521         if ($exact) {
    1522             $n = '';
    1523         } else {
    1524             $n = '%';
    1525         }
    1526         if (!$sentence) {
    1527             $s_array = explode(' ',$s);
    1528             $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))';
    1529             for ( $i = 1; $i < count($s_array); $i = $i + 1) {
    1530                 $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))';
    1531             }
    1532             $search .= ' OR (post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\')';
    1533             $search .= ')';
    1534         } else {
    1535             $search = ' AND ((post_title LIKE \''.$n.$s.$n.'\') OR (post_content LIKE \''.$n.$s.$n.'\'))';
    1536         }
    1537     }
    1538 
    1539     // Category stuff
    1540 
    1541     if ((empty($cat)) || ($cat == 'all') || ($cat == '0') ||
    1542         // Bypass cat checks if fetching specific posts
    1543         (
    1544          intval($year) || intval($monthnum) || intval($day) || intval($w) ||
    1545          intval($p) || !empty($name) || !empty($s)
    1546          )
    1547         ) {
    1548         $whichcat='';
    1549     } else {
    1550         $cat = ''.urldecode($cat).'';
    1551         $cat = addslashes_gpc($cat);
    1552         if (stristr($cat,'-')) {
    1553             // Note: if we have a negative, we ignore all the positives. It must
    1554             // always mean 'everything /except/ this one'. We should be able to do
    1555             // multiple negatives but we don't :-(
    1556             $eq = '!=';
    1557             $andor = 'AND';
    1558             $cat = explode('-',$cat);
    1559             $cat = intval($cat[1]);
    1560         } else {
    1561             $eq = '=';
    1562             $andor = 'OR';
    1563         }
    1564         $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) ";
    1565         $cat_array = explode(' ',$cat);
    1566         $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]);
    1567         $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');
    1568         for ($i = 1; $i < (count($cat_array)); $i = $i + 1) {
    1569             $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]);
    1570             $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');
    1571         }
    1572         $whichcat .= ')';
    1573         if ($eq == '!=') {
    1574             $cat = '-'.$cat; // Put back the knowledge that we are excluding a category.
    1575         }
    1576     }
    1577 
    1578     // Category stuff for nice URIs
    1579 
    1580     if ('' != $category_name) {
    1581         if (stristr($category_name,'/')) {
    1582             $category_name = explode('/',$category_name);
    1583             if ($category_name[count($category_name)-1]) {
    1584                 $category_name = $category_name[count($category_name)-1]; // no trailing slash
    1585             } else {
    1586                 $category_name = $category_name[count($category_name)-2]; // there was a trailling slash
    1587             }
    1588         }
    1589         $category_name = preg_replace('|[^a-z0-9-_]|i', '', $category_name);
    1590         $tables = ", $wpdb->post2cat, $wpdb->categories";
    1591         $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
    1592         $whichcat = " AND (category_nicename = '$category_name'";
    1593         $cat = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_name'");
    1594         $whichcat .= get_category_children($cat, " OR category_id = ");
    1595         $whichcat .= ")";
    1596     }
    1597 
    1598     // Author/user stuff
    1599 
    1600     if ((empty($author)) || ($author == 'all') || ($author == '0')) {
    1601         $whichauthor='';
    1602     } else {
    1603         $author = ''.urldecode($author).'';
    1604         $author = addslashes_gpc($author);
    1605         if (stristr($author, '-')) {
    1606             $eq = '!=';
    1607             $andor = 'AND';
    1608             $author = explode('-', $author);
    1609             $author = ''.intval($author[1]);
    1610         } else {
    1611             $eq = '=';
    1612             $andor = 'OR';
    1613         }
    1614         $author_array = explode(' ', $author);
    1615         $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
    1616         for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
    1617             $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
    1618         }
    1619         $whichauthor .= ')';
    1620     }
    1621 
    1622     // Author stuff for nice URIs
    1623 
    1624     if ('' != $author_name) {
    1625         if (stristr($author_name,'/')) {
    1626             $author_name = explode('/',$author_name);
    1627             if ($author_name[count($author_name)-1]) {
    1628                 $author_name = $author_name[count($author_name)-1];#no trailing slash
    1629             } else {
    1630                 $author_name = $author_name[count($author_name)-2];#there was a trailling slash
    1631             }
    1632         }
    1633         $author_name = preg_replace('|[^a-z0-9-_]|', '', strtolower($author_name));
    1634         $author = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$author_name."'");
    1635         $whichauthor .= ' AND (post_author = '.intval($author).')';
    1636     }
    1637 
    1638     $where .= $search.$whichcat.$whichauthor;
    1639 
    1640     if ((empty($order)) || ((strtoupper($order) != 'ASC') && (strtoupper($order) != 'DESC'))) {
    1641         $order='DESC';
    1642     }
    1643 
    1644     // Order by
    1645     if (empty($orderby)) {
    1646         $orderby='date '.$order;
    1647     } else {
    1648         // Used to filter values
    1649         $allowed_keys = array('author','date','category','title');
    1650         $orderby = urldecode($orderby);
    1651         $orderby = addslashes_gpc($orderby);
    1652         $orderby_array = explode(' ',$orderby);
    1653         if (!in_array($orderby_array[0],$allowed_keys)) {
    1654             $orderby_array[0] = 'date';
    1655         }
    1656         $orderby = $orderby_array[0].' '.$order;
    1657         if (count($orderby_array)>1) {
    1658             for ($i = 1; $i < (count($orderby_array)); $i = $i + 1) {
    1659                 // Only allow certain values for safety
    1660                 if (in_array($orderby_array[$i],$allowed_keys)) {
    1661                     $orderby .= ',post_'.$orderby_array[$i].' '.$order;
    1662                 }
    1663             }
    1664         }
    1665     }
    1666 
    1667     if ((!$whichcat) && (!$m) && (!$p) && (!$w) && (!$s) && empty($poststart) && empty($postend)) {
    1668         if ($what_to_show == 'posts') {
    1669             $limits = ' LIMIT '.$posts_per_page;
    1670         } elseif ($what_to_show == 'days' && empty($monthnum) && empty($year) && empty($day)) {
    1671             $lastpostdate = get_lastpostdate();
    1672             $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate);
    1673             $lastpostdate = mysql2date('U',$lastpostdate);
    1674             $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($posts_per_page-1) * 86400)));
    1675             $where .= " AND post_date > '$otherdate'";
    1676         }
    1677     }
    1678 
    1679     if ( !empty($postend) && ($postend > $poststart) && (!$m) && empty($monthnum) && empty($year) && empty($day) &&(!$w) && (!$whichcat) && (!$s) && (!$p)) {
    1680         if ($what_to_show == 'posts') {
    1681             $poststart = intval($poststart);
    1682             $postend = intval($postend);
    1683             $limposts = $postend - $poststart;
    1684             $limits = ' LIMIT '.$poststart.','.$limposts;
    1685         } elseif ($what_to_show == 'days') {
    1686             $poststart = intval($poststart);
    1687             $postend = intval($postend);
    1688             $limposts = $postend - $poststart;
    1689             $lastpostdate = get_lastpostdate();
    1690             $lastpostdate = mysql2date('Y-m-d 00:00:00',$lastpostdate);
    1691             $lastpostdate = mysql2date('U',$lastpostdate);
    1692             $startdate = date('Y-m-d H:i:s', ($lastpostdate - (($poststart -1) * 86400)));
    1693             $otherdate = date('Y-m-d H:i:s', ($lastpostdate - (($postend -1) * 86400)));
    1694             $where .= " AND post_date > '$otherdate' AND post_date < '$startdate'";
    1695         }
    1696     } else {
    1697         if (($what_to_show == 'posts') && (! is_single()) && (!$more)) {
    1698             if ($pagenow != 'post.php') {
    1699                 $pgstrt = '';
    1700                 if ($paged) {
    1701                     $pgstrt = (intval($paged) -1) * $posts_per_page . ', ';
    1702                 }
    1703                 $limits = 'LIMIT '.$pgstrt.$posts_per_page;
    1704             } else {
    1705                 if (($m) || ($p) || ($w) || ($s) || ($whichcat)) {
    1706                     $limits = '';
    1707                 } else {
    1708                     $pgstrt = '';
    1709                     if ($paged) {
    1710                         $pgstrt = (intval($paged) -1) * $posts_per_page . ', ';
    1711                     }
    1712                     $limits = 'LIMIT '.$pgstrt.$posts_per_page;
    1713                 }
    1714             }
    1715         }
    1716         elseif (($m) || ($p) || ($w) || ($s) || ($whichcat) || ($author) || $monthnum || $year || $day) {
    1717             $limits = '';
    1718         }
    1719     }
    1720 
    1721     if ($p == 'all') {
    1722         $where = '';
    1723     }
    1724 
    1725     $now = gmdate('Y-m-d H:i:59');
    1726 
    1727     if ($pagenow != 'post.php' && $pagenow != 'edit.php') {
    1728         if ((empty($poststart)) || (empty($postend)) || !($postend > $poststart)) {
    1729             $where .= " AND post_date_gmt <= '$now'";
    1730         }
    1731 
    1732         $distinct = 'DISTINCT';
    1733     }
    1734     $where .= ' AND (post_status = "publish"';
    1735 
    1736     // Get private posts
    1737     if (isset($user_ID) && ('' != intval($user_ID)))
    1738         $where .= " OR post_author = $user_ID AND post_status != 'draft')";
    1739     else
    1740         $where .= ')';
    1741 
    1742     $where = apply_filters('posts_where', $where);
    1743     $where .= " GROUP BY $wpdb->posts.ID";
    1744     $request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1".$where." ORDER BY post_$orderby $limits";
    1745 
    1746 
    1747     if ($preview) {
    1748         $request = 'SELECT 1-1'; // dummy mysql query for the preview
    1749         // little funky fix for IEwin, rawk on that code
    1750         $is_winIE = ((preg_match('/MSIE/',$HTTP_USER_AGENT)) && (preg_match('/Win/',$HTTP_USER_AGENT)));
    1751         if (($is_winIE) && (!isset($IEWin_bookmarklet_fix))) {
    1752             $preview_content =  preg_replace('/\%u([0-9A-F]{4,4})/e',  "'&#'.base_convert('\\1',16,10).';'", $preview_content);
    1753         }
    1754     }
    1755 
    1756     // error_log("$request");
    1757     // echo $request;
    1758     $posts = $wpdb->get_results($request);
    1759     $posts = apply_filters('the_posts', $posts);
    1760 
    1761     update_post_caches($posts);
    1762     return $posts;
     1441    global $wp_query;
     1442
     1443    return $wp_query->query($query);
    17631444}
    17641445
     
    18531534
    18541535function is_single () {
    1855     global $wp_query_state;
    1856 
    1857     return $wp_query_state->single;
     1536    global $wp_query;
     1537
     1538    return $wp_query->is_single;
    18581539}
    18591540
    18601541function is_archive () {
    1861     global $wp_query_state;
    1862 
    1863     return $wp_query_state->archive;
     1542    global $wp_query;
     1543
     1544    return $wp_query->is_archive;
    18641545}
    18651546
    18661547function is_date () {
    1867     global $wp_query_state;
    1868 
    1869     return $wp_query_state->date;
     1548    global $wp_query;
     1549
     1550    return $wp_query->is_date;
    18701551}
    18711552
    18721553function is_author () {
    1873     global $wp_query_state;
    1874 
    1875     return $wp_query_state->author;
     1554    global $wp_query;
     1555
     1556    return $wp_query->is_author;
    18761557}
    18771558
    18781559function is_category () {
    1879     global $wp_query_state;
    1880 
    1881     return $wp_query_state->category;
     1560    global $wp_query;
     1561
     1562    return $wp_query->is_category;
    18821563}
    18831564
    18841565function is_search () {
    1885     global $wp_query_state;
    1886 
    1887     return $wp_query_state->search;
     1566    global $wp_query;
     1567
     1568    return $wp_query->is_search;
    18881569}
    18891570
    18901571function is_feed () {
    1891     global $wp_query_state;
    1892 
    1893     return $wp_query_state->search;
     1572    global $wp_query;
     1573
     1574    return $wp_query->is_feed;
    18941575}
    18951576
    18961577function is_home () {
    1897     global $wp_query_state;
    1898 
    1899     return $wp_query_state->home;
     1578    global $wp_query;
     1579
     1580    return $wp_query->is_home;
     1581}
     1582
     1583function have_posts() {
     1584    global $wp_query;
     1585
     1586    return $wp_query->have_posts();
     1587}
     1588
     1589function the_post() {
     1590    start_wp(true);
    19001591}
    19011592
Note: See TracChangeset for help on using the changeset viewer.