summaryrefslogtreecommitdiffstats
path: root/wp-inst/wp-includes/classes.php
diff options
context:
space:
mode:
authormatt <matt@7be80a69-a1ef-0310-a953-fb0f7c49ff36>2006-05-15 03:19:56 +0000
committermatt <matt@7be80a69-a1ef-0310-a953-fb0f7c49ff36>2006-05-15 03:19:56 +0000
commit034694eb53dd71a980745f5594509890fd761998 (patch)
treec85517e8af3fce163f1d183afdf7c9ac7094d84e /wp-inst/wp-includes/classes.php
parenta72266931b88c8fa300d2e29a69a2bb38a70654a (diff)
downloadwordpress-mu-034694eb53dd71a980745f5594509890fd761998.tar.gz
wordpress-mu-034694eb53dd71a980745f5594509890fd761998.tar.xz
wordpress-mu-034694eb53dd71a980745f5594509890fd761998.zip
Lots and lots of changes.
git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@543 7be80a69-a1ef-0310-a953-fb0f7c49ff36
Diffstat (limited to 'wp-inst/wp-includes/classes.php')
-rw-r--r--wp-inst/wp-includes/classes.php1716
1 files changed, 348 insertions, 1368 deletions
diff --git a/wp-inst/wp-includes/classes.php b/wp-inst/wp-includes/classes.php
index 03d74bb..3952cb5 100644
--- a/wp-inst/wp-includes/classes.php
+++ b/wp-inst/wp-includes/classes.php
@@ -1,769 +1,5 @@
<?php
-class WP_Query {
- var $query;
- var $query_vars;
- var $queried_object;
- var $queried_object_id;
- var $request;
-
- var $posts;
- var $post_count = 0;
- var $current_post = -1;
- var $in_the_loop = false;
- var $post;
-
- var $is_single = false;
- var $is_preview = false;
- var $is_page = false;
- var $is_archive = false;
- var $is_date = false;
- var $is_year = false;
- var $is_month = false;
- var $is_day = false;
- var $is_time = false;
- var $is_author = false;
- var $is_category = false;
- var $is_search = false;
- var $is_feed = false;
- var $is_trackback = false;
- var $is_home = false;
- var $is_404 = false;
- var $is_comments_popup = false;
- var $is_admin = false;
- var $is_attachment = false;
-
- function init_query_flags() {
- $this->is_single = false;
- $this->is_page = false;
- $this->is_archive = false;
- $this->is_date = false;
- $this->is_year = false;
- $this->is_month = false;
- $this->is_day = false;
- $this->is_time = false;
- $this->is_author = false;
- $this->is_category = false;
- $this->is_search = false;
- $this->is_feed = false;
- $this->is_trackback = false;
- $this->is_home = false;
- $this->is_404 = false;
- $this->is_paged = false;
- $this->is_admin = false;
- $this->is_attachment = false;
- }
-
- function init () {
- unset($this->posts);
- unset($this->query);
- unset($this->query_vars);
- unset($this->queried_object);
- unset($this->queried_object_id);
- $this->post_count = 0;
- $this->current_post = -1;
- $this->in_the_loop = false;
-
- $this->init_query_flags();
- }
-
- // Reparse the query vars.
- function parse_query_vars() {
- $this->parse_query('');
- }
-
- // Parse a query string and set query type booleans.
- function parse_query ($query) {
- if ( !empty($query) || !isset($this->query) ) {
- $this->init();
- parse_str($query, $qv);
- $this->query = $query;
- $this->query_vars = $qv;
- }
-
- if ('404' == $qv['error']) {
- $this->is_404 = true;
- if ( !empty($query) ) {
- do_action('parse_query', array(&$this));
- }
- return;
- }
-
- $qv['m'] = (int) $qv['m'];
- $qv['p'] = (int) $qv['p'];
-
- // Compat. Map subpost to attachment.
- if ( '' != $qv['subpost'] )
- $qv['attachment'] = $qv['subpost'];
- if ( '' != $qv['subpost_id'] )
- $qv['attachment_id'] = $qv['subpost_id'];
-
- if ( ('' != $qv['attachment']) || (int) $qv['attachment_id'] ) {
- $this->is_single = true;
- $this->is_attachment = true;
- } elseif ('' != $qv['name']) {
- $this->is_single = true;
- } elseif ( $qv['p'] ) {
- $this->is_single = true;
- } elseif (('' != $qv['hour']) && ('' != $qv['minute']) &&('' != $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day'])) {
- // If year, month, day, hour, minute, and second are set, a single
- // post is being queried.
- $this->is_single = true;
- } elseif ('' != $qv['static'] || '' != $qv['pagename'] || '' != $qv['page_id']) {
- $this->is_page = true;
- $this->is_single = false;
- } elseif (!empty($qv['s'])) {
- $this->is_search = true;
- } else {
- // Look for archive queries. Dates, categories, authors.
-
- if ( (int) $qv['second']) {
- $this->is_time = true;
- $this->is_date = true;
- }
-
- if ( (int) $qv['minute']) {
- $this->is_time = true;
- $this->is_date = true;
- }
-
- if ( (int) $qv['hour']) {
- $this->is_time = true;
- $this->is_date = true;
- }
-
- if ( (int) $qv['day']) {
- if (! $this->is_date) {
- $this->is_day = true;
- $this->is_date = true;
- }
- }
-
- if ( (int) $qv['monthnum']) {
- if (! $this->is_date) {
- $this->is_month = true;
- $this->is_date = true;
- }
- }
-
- if ( (int) $qv['year']) {
- if (! $this->is_date) {
- $this->is_year = true;
- $this->is_date = true;
- }
- }
-
- if ( (int) $qv['m']) {
- $this->is_date = true;
- if (strlen($qv['m']) > 9) {
- $this->is_time = true;
- } else if (strlen($qv['m']) > 7) {
- $this->is_day = true;
- } else if (strlen($qv['m']) > 5) {
- $this->is_month = true;
- } else {
- $this->is_year = true;
- }
- }
-
- if ('' != $qv['w']) {
- $this->is_date = true;
- }
-
- if (empty($qv['cat']) || ($qv['cat'] == '0')) {
- $this->is_category = false;
- } else {
- if (stristr($qv['cat'],'-')) {
- $this->is_category = false;
- } else {
- $this->is_category = true;
- }
- }
-
- if ('' != $qv['category_name']) {
- $this->is_category = true;
- }
-
- if ((empty($qv['author'])) || ($qv['author'] == '0')) {
- $this->is_author = false;
- } else {
- $this->is_author = true;
- }
-
- if ('' != $qv['author_name']) {
- $this->is_author = true;
- }
-
- if ( ($this->is_date || $this->is_author || $this->is_category)) {
- $this->is_archive = true;
- }
- }
-
- if ('' != $qv['feed']) {
- $this->is_feed = true;
- }
-
- if ('' != $qv['tb']) {
- $this->is_trackback = true;
- }
-
- if ('' != $qv['paged']) {
- $this->is_paged = true;
- }
-
- if ('' != $qv['comments_popup']) {
- $this->is_comments_popup = true;
- }
-
- //if we're previewing inside the write screen
- if ('' != $qv['preview']) {
- $this->is_preview = true;
- }
-
- if (strstr($_SERVER['PHP_SELF'], 'wp-admin/')) {
- $this->is_admin = true;
- }
-
- if ( ! ($this->is_attachment || $this->is_archive || $this->is_single || $this->is_page || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup)) {
- $this->is_home = true;
- }
-
- if ( !empty($query) ) {
- do_action('parse_query', array(&$this));
- }
- }
-
- function set_404() {
- $this->init_query_flags();
- $this->is_404 = true;
- }
-
- function get($query_var) {
- if (isset($this->query_vars[$query_var])) {
- return $this->query_vars[$query_var];
- }
-
- return '';
- }
-
- function set($query_var, $value) {
- $this->query_vars[$query_var] = $value;
- }
-
- function &get_posts() {
- global $wpdb, $pagenow, $user_ID;
-
- do_action('pre_get_posts', array(&$this));
-
- // Shorthand.
- $q = &$this->query_vars;
-
- // First let's clear some variables
- $whichcat = '';
- $whichauthor = '';
- $whichpage = '';
- $result = '';
- $where = '';
- $limits = '';
- $distinct = '';
- $join = '';
-
- if ( !isset($q['post_type']) )
- $q['post_type'] = 'post';
- $post_type = $q['post_type'];
- if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
- $q['posts_per_page'] = get_settings('posts_per_page');
- if ( !isset($q['what_to_show']) )
- $q['what_to_show'] = get_settings('what_to_show');
- if ( isset($q['showposts']) && $q['showposts'] ) {
- $q['showposts'] = (int) $q['showposts'];
- $q['posts_per_page'] = $q['showposts'];
- }
- if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
- $q['posts_per_page'] = $q['posts_per_archive_page'];
- if ( !isset($q['nopaging']) ) {
- if ($q['posts_per_page'] == -1) {
- $q['nopaging'] = true;
- } else {
- $q['nopaging'] = false;
- }
- }
- if ( $this->is_feed ) {
- $q['posts_per_page'] = get_settings('posts_per_rss');
- $q['what_to_show'] = 'posts';
- }
-
- if (isset($q['page'])) {
- $q['page'] = trim($q['page'], '/');
- $q['page'] = (int) $q['page'];
- }
-
- $add_hours = intval(get_settings('gmt_offset'));
- $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours));
- $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
-
- // If a month is specified in the querystring, load that month
- if ( (int) $q['m'] ) {
- $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
- $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4);
- if (strlen($q['m'])>5)
- $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2);
- if (strlen($q['m'])>7)
- $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2);
- if (strlen($q['m'])>9)
- $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2);
- if (strlen($q['m'])>11)
- $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2);
- if (strlen($q['m'])>13)
- $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2);
- }
-
- if ( (int) $q['hour'] ) {
- $q['hour'] = '' . intval($q['hour']);
- $where .= " AND HOUR(post_date)='" . $q['hour'] . "'";
- }
-
- if ( (int) $q['minute'] ) {
- $q['minute'] = '' . intval($q['minute']);
- $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'";
- }
-
- if ( (int) $q['second'] ) {
- $q['second'] = '' . intval($q['second']);
- $where .= " AND SECOND(post_date)='" . $q['second'] . "'";
- }
-
- if ( (int) $q['year'] ) {
- $q['year'] = '' . intval($q['year']);
- $where .= " AND YEAR(post_date)='" . $q['year'] . "'";
- }
-
- if ( (int) $q['monthnum'] ) {
- $q['monthnum'] = '' . intval($q['monthnum']);
- $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'";
- }
-
- if ( (int) $q['day'] ) {
- $q['day'] = '' . intval($q['day']);
- $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'";
- }
-
- // Compat. Map subpost to attachment.
- if ( '' != $q['subpost'] )
- $q['attachment'] = $q['subpost'];
- if ( '' != $q['subpost_id'] )
- $q['attachment_id'] = $q['subpost_id'];
-
- if ('' != $q['name']) {
- $q['name'] = sanitize_title($q['name']);
- $where .= " AND post_name = '" . $q['name'] . "'";
- } else if ('' != $q['pagename']) {
- $reqpage = get_page_by_path($q['pagename']);
- $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
- $page_paths = '/' . trim($q['pagename'], '/');
- $q['pagename'] = sanitize_title(basename($page_paths));
- $q['name'] = $q['pagename'];
-
- $where .= " AND (ID = '$reqpage')";
- } elseif ('' != $q['attachment']) {
- $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
- $attach_paths = '/' . trim($q['attachment'], '/');
- $q['attachment'] = sanitize_title(basename($attach_paths));
- $q['name'] = $q['attachment'];
- $where .= " AND post_name = '" . $q['attachment'] . "'";
- }
-
- if ( (int) $q['w'] ) {
- $q['w'] = ''.intval($q['w']);
- $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'";
- }
-
- if ( intval($q['comments_popup']) )
- $q['p'] = intval($q['comments_popup']);
-
- // If a attachment is requested by number, let it supercede any post number.
- if ( ($q['attachment_id'] != '') && (intval($q['attachment_id']) != 0) )
- $q['p'] = (int) $q['attachment_id'];
-
- // If a post number is specified, load that post
- if (($q['p'] != '') && intval($q['p']) != 0) {
- $q['p'] = (int) $q['p'];
- $where = ' AND ID = ' . $q['p'];
- }
-
- if (($q['page_id'] != '') && (intval($q['page_id']) != 0)) {
- $q['page_id'] = intval($q['page_id']);
- $q['p'] = $q['page_id'];
- $where = ' AND ID = '.$q['page_id'];
- }
-
- // If a search pattern is specified, load the posts that match
- if (!empty($q['s'])) {
- $q['s'] = addslashes_gpc($q['s']);
- $search = ' AND (';
- $q['s'] = preg_replace('/, +/', ' ', $q['s']);
- $q['s'] = str_replace(',', ' ', $q['s']);
- $q['s'] = str_replace('"', ' ', $q['s']);
- $q['s'] = trim($q['s']);
- if ($q['exact']) {
- $n = '';
- } else {
- $n = '%';
- }
- if (!$q['sentence']) {
- $s_array = explode(' ',$q['s']);
- $q['search_terms'] = $s_array;
- $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))';
- for ( $i = 1; $i < count($s_array); $i = $i + 1) {
- $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))';
- }
- $search .= ' OR (post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\')';
- $search .= ')';
- } else {
- $search = ' AND ((post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\'))';
- }
- }
-
- // Category stuff
-
- if ((empty($q['cat'])) || ($q['cat'] == '0') ||
- // Bypass cat checks if fetching specific posts
- ( $this->is_single || $this->is_page )) {
- $whichcat='';
- } else {
- $q['cat'] = ''.urldecode($q['cat']).'';
- $q['cat'] = addslashes_gpc($q['cat']);
- if (stristr($q['cat'],'-')) {
- // Note: if we have a negative, we ignore all the positives. It must
- // always mean 'everything /except/ this one'. We should be able to do
- // multiple negatives but we don't :-(
- $eq = '!=';
- $andor = 'AND';
- $q['cat'] = explode('-',$q['cat']);
- $q['cat'] = intval($q['cat'][1]);
- } else {
- $eq = '=';
- $andor = 'OR';
- }
- $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) ";
- $cat_array = preg_split('/[,\s]+/', $q['cat']);
- $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]);
- $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');
- for ($i = 1; $i < (count($cat_array)); $i = $i + 1) {
- $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]);
- $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');
- }
- $whichcat .= ')';
- if ($eq == '!=') {
- $q['cat'] = '-'.$q['cat']; // Put back the knowledge that we are excluding a category.
- }
- }
-
- // Category stuff for nice URIs
-
- global $cache_categories;
- if ('' != $q['category_name']) {
- $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
- $q['category_name'] = sanitize_title(basename($cat_paths));
- $cat_paths = explode('/', $cat_paths);
- foreach($cat_paths as $pathdir)
- $cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);
-
- $all_cat_ids = get_all_category_ids();
- $q['cat'] = 0; $partial_match = 0;
- foreach ( $all_cat_ids as $cat_id ) {
- $cat = get_category($cat_id);
- if ( $cat->fullpath == $cat_path ) {
- $q['cat'] = $cat_id;
- break;
- } elseif ( $cat->category_nicename == $q['category_name'] ) {
- $partial_match = $cat_id;
- }
- }
-
- //if we don't match the entire hierarchy fallback on just matching the nicename
- if (!$q['cat'] && $partial_match) {
- $q['cat'] = $partial_match;
- }
-
- $tables = ", $wpdb->post2cat, $wpdb->categories";
- $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) ";
- $whichcat = " AND (category_id = '" . $q['cat'] . "'";
- $whichcat .= get_category_children($q['cat'], " OR category_id = ");
- $whichcat .= ")";
- }
-
- // Author/user stuff
-
- if ((empty($q['author'])) || ($q['author'] == '0')) {
- $whichauthor='';
- } else {
- $q['author'] = ''.urldecode($q['author']).'';
- $q['author'] = addslashes_gpc($q['author']);
- if (stristr($q['author'], '-')) {
- $eq = '!=';
- $andor = 'AND';
- $q['author'] = explode('-', $q['author']);
- $q['author'] = ''.intval($q['author'][1]);
- } else {
- $eq = '=';
- $andor = 'OR';
- }
- $author_array = preg_split('/[,\s]+/', $q['author']);
- $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
- for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
- $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
- }
- $whichauthor .= ')';
- }
-
- // Author stuff for nice URIs
-
- if ('' != $q['author_name']) {
- if (stristr($q['author_name'],'/')) {
- $q['author_name'] = explode('/',$q['author_name']);
- if ($q['author_name'][count($q['author_name'])-1]) {
- $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
- } else {
- $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
- }
- }
- $q['author_name'] = sanitize_title($q['author_name']);
- $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
- $whichauthor .= ' AND (post_author = '.intval($q['author']).')';
- }
-
- $where .= $search.$whichcat.$whichauthor;
-
- if ((empty($q['order'])) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC'))) {
- $q['order']='DESC';
- }
-
- // Order by
- if (empty($q['orderby'])) {
- $q['orderby'] = 'post_date '.$q['order'];
- } else {
- // Used to filter values
- $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order');
- $q['orderby'] = urldecode($q['orderby']);
- $q['orderby'] = addslashes_gpc($q['orderby']);
- $orderby_array = explode(' ',$q['orderby']);
- if ( empty($orderby_array) )
- $orderby_array[] = $q['orderby'];
- $q['orderby'] = '';
- for ($i = 0; $i < count($orderby_array); $i++) {
- // Only allow certain values for safety
- $orderby = $orderby_array[$i];
- if ( 'menu_order' != $orderby )
- $orderby = 'post_' . $orderby;
- if ( in_array($orderby_array[$i], $allowed_keys) )
- $q['orderby'] .= (($i == 0) ? '' : ',') . "$orderby {$q['order']}";
- }
- if ( empty($q['orderby']) )
- $q['orderby'] = 'post_date '.$q['order'];
- }
-
- //$now = gmdate('Y-m-d H:i:59');
-
- //only select past-dated posts, except if a logged in user is viewing a single: then, if they
- //can edit the post, we let them through
- //if ($pagenow != 'post.php' && $pagenow != 'edit.php' && !($this->is_single && $user_ID)) {
- // $where .= " AND post_date_gmt <= '$now'";
- // $distinct = 'DISTINCT';
- //}
-
- if ( $this->is_attachment ) {
- $where .= ' AND (post_type = "attachment")';
- } elseif ($this->is_page) {
- $where .= ' AND (post_type = "page")';
- } elseif ($this->is_single) {
- $where .= ' AND (post_type = "post")';
- } else {
- $where .= " AND (post_type = '$post_type' AND (post_status = 'publish'";
-
- if ( is_admin() )
- $where .= " OR post_status = 'future' OR post_status = 'draft'";
- else
- $distinct = 'DISTINCT';
-
- if ( is_user_logged_in() )
- $where .= " OR post_author = $user_ID AND post_status = 'private'))";
- else
- $where .= '))';
- }
-
- // Apply filters on where and join prior to paging so that any
- // manipulations to them are reflected in the paging by day queries.
- $where = apply_filters('posts_where', $where);
- $join = apply_filters('posts_join', $join);
-
- // Paging
- if (empty($q['nopaging']) && ! $this->is_single) {
- $page = $q['paged'];
- if (empty($page)) {
- $page = 1;
- }
-
- if (($q['what_to_show'] == 'posts')) {
- $pgstrt = '';
- $pgstrt = (intval($page) -1) * $q['posts_per_page'] . ', ';
- $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
- } elseif ($q['what_to_show'] == 'days') {
- $startrow = $q['posts_per_page'] * (intval($page)-1);
- $start_date = $wpdb->get_var("SELECT max(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $startrow,1");
- $endrow = $startrow + $q['posts_per_page'] - 1;
- $end_date = $wpdb->get_var("SELECT min(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $endrow,1");
-
- if ($page > 1) {
- $where .= " AND post_date >= '$end_date' AND post_date <= '$start_date'";
- } else {
- $where .= " AND post_date >= '$end_date'";
- }
- }
- }
-
- // Apply post-paging filters on where and join. Only plugins that
- // manipulate paging queries should use these hooks.
- $where = apply_filters('posts_where_paged', $where);
- $groupby = " $wpdb->posts.ID ";
- $groupby = apply_filters('posts_groupby', $groupby);
- $join = apply_filters('posts_join_paged', $join);
- $orderby = apply_filters('posts_orderby', $q['orderby']);
- $request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1" . $where . " GROUP BY " . $groupby . " ORDER BY " . $orderby . " $limits";
- $this->request = apply_filters('posts_request', $request);
-
- $this->posts = $wpdb->get_results($this->request);
-
- // Check post status to determine if post should be displayed.
- if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
- $status = get_post_status($this->posts[0]);
- //$type = get_post_type($this->posts[0]);
- if ( ('publish' != $status) ) {
- if ( ! is_user_logged_in() ) {
- // User must be logged in to view unpublished posts.
- $this->posts = array();
- } else {
- if ('draft' == $status) {
- // User must have edit permissions on the draft to preview.
- if (! current_user_can('edit_post', $this->posts[0]->ID)) {
- $this->posts = array();
- } else {
- $this->is_preview = true;
- $this->posts[0]->post_date = current_time('mysql');
- }
- } else if ('future' == $status) {
- $this->is_preview = true;
- if (!current_user_can('edit_post', $this->posts[0]->ID)) {
- $this->posts = array ( );
- }
- } else {
- if (! current_user_can('read_post', $this->posts[0]->ID))
- $this->posts = array();
- }
- }
- }
- }
-
- update_post_caches($this->posts);
-
- $this->posts = apply_filters('the_posts', $this->posts);
- $this->post_count = count($this->posts);
- if ($this->post_count > 0) {
- $this->post = $this->posts[0];
- }
-
- return $this->posts;
- }
-
- function next_post() {
-
- $this->current_post++;
-
- $this->post = $this->posts[$this->current_post];
- return $this->post;
- }
-
- function the_post() {
- global $post;
- $this->in_the_loop = true;
- $post = $this->next_post();
- setup_postdata($post);
-
- if ( $this->current_post == 0 ) // loop has just started
- do_action('loop_start');
- }
-
- function have_posts() {
- if ($this->current_post + 1 < $this->post_count) {
- return true;
- } elseif ($this->current_post + 1 == $this->post_count) {
- do_action('loop_end');
- // Do some cleaning up after the loop
- $this->rewind_posts();
- }
-
- $this->in_the_loop = false;
- return false;
- }
-
- function rewind_posts() {
- $this->current_post = -1;
- if ($this->post_count > 0) {
- $this->post = $this->posts[0];
- }
- }
-
- function &query($query) {
- $this->parse_query($query);
- return $this->get_posts();
- }
-
- function get_queried_object() {
- if (isset($this->queried_object)) {
- return $this->queried_object;
- }
-
- $this->queried_object = NULL;
- $this->queried_object_id = 0;
-
- if ($this->is_category) {
- $cat = $this->get('cat');
- $category = &get_category($cat);
- $this->queried_object = &$category;
- $this->queried_object_id = $cat;
- } else if ($this->is_single) {
- $this->queried_object = $this->post;
- $this->queried_object_id = $this->post->ID;
- } else if ($this->is_page) {
- $this->queried_object = $this->post;
- $this->queried_object_id = $this->post->ID;
- } else if ($this->is_author) {
- $author_id = $this->get('author');
- $author = get_userdata($author_id);
- $this->queried_object = $author;
- $this->queried_object_id = $author_id;
- }
-
- return $this->queried_object;
- }
-
- function get_queried_object_id() {
- $this->get_queried_object();
-
- if (isset($this->queried_object_id)) {
- return $this->queried_object_id;
- }
-
- return 0;
- }
-
- function WP_Query ($query = '') {
- if (! empty($query)) {
- $this->query($query);
- }
- }
-}
-
class retrospam_mgr {
var $spam_words;
var $comments_list;
@@ -804,7 +40,7 @@ class retrospam_mgr {
if ( empty( $word ) )
continue;
$fulltext = strtolower($comment->email.' '.$comment->url.' '.$comment->ip.' '.$comment->text);
- if( strpos( $fulltext, strtolower($word) ) != FALSE ) {
+ if( false !== strpos( $fulltext, strtolower($word) ) ) {
$this->found_comments[] = $comment->ID;
break;
}
@@ -836,606 +72,6 @@ class retrospam_mgr {
}
-class WP_Rewrite {
- var $permalink_structure;
- var $category_base;
- var $category_structure;
- var $author_base = 'author';
- var $author_structure;
- var $date_structure;
- var $page_structure;
- var $search_base = 'search';
- var $search_structure;
- var $comments_base = 'comments';
- var $feed_base = 'feed';
- var $comments_feed_structure;
- var $feed_structure;
- var $front;
- var $root = '';
- var $index = 'index.php';
- var $matches = '';
- var $rules;
- var $use_verbose_rules = false;
- var $rewritecode =
- array(
- '%year%',
- '%monthnum%',
- '%day%',
- '%hour%',
- '%minute%',
- '%second%',
- '%postname%',
- '%post_id%',
- '%category%',
- '%author%',
- '%pagename%',
- '%search%'
- );
-
- var $rewritereplace =
- array(
- '([0-9]{4})',
- '([0-9]{1,2})',
- '([0-9]{1,2})',
- '([0-9]{1,2})',
- '([0-9]{1,2})',
- '([0-9]{1,2})',
- '([^/]+)',
- '([0-9]+)',
- '(.+?)',
- '([^/]+)',
- '([^/]+)',
- '(.+)'
- );
-
- var $queryreplace =
- array (
- 'year=',
- 'monthnum=',
- 'day=',
- 'hour=',
- 'minute=',
- 'second=',
- 'name=',
- 'p=',
- 'category_name=',
- 'author_name=',
- 'pagename=',
- 's='
- );
-
- var $feeds = array ('feed', 'rdf', 'rss', 'rss2', 'atom');
-
- function using_permalinks() {
- if (empty($this->permalink_structure))
- return false;
- else
- return true;
- }
-
- function using_index_permalinks() {
- if (empty($this->permalink_structure)) {
- return false;
- }
-
- // If the index is not in the permalink, we're using mod_rewrite.
- if (preg_match('#^/*' . $this->index . '#', $this->permalink_structure)) {
- return true;
- }
-
- return false;
- }
-
- function using_mod_rewrite_permalinks() {
- if ( $this->using_permalinks() && ! $this->using_index_permalinks())
- return true;
- else
- return false;
- }
-
- function preg_index($number) {
- $match_prefix = '$';
- $match_suffix = '';
-
- if (! empty($this->matches)) {
- $match_prefix = '$' . $this->matches . '[';
- $match_suffix = ']';
- }
-
- return "$match_prefix$number$match_suffix";
- }
-
- function page_rewrite_rules() {
- $uris = get_settings('page_uris');
- $attachment_uris = get_settings('page_attachment_uris');
-
- $rewrite_rules = array();
- $page_structure = $this->get_page_permastruct();
- if( is_array( $attachment_uris ) ) {
- foreach ($attachment_uris as $uri => $pagename) {
- $this->add_rewrite_tag('%pagename%', "($uri)", 'attachment=');
- $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure));
- }
- }
- if( is_array( $uris ) ) {
- foreach ($uris as $uri => $pagename) {
- $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename=');
- $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure));
- }
- }
-
- return $rewrite_rules;
- }
-
- function get_date_permastruct() {
- if (isset($this->date_structure)) {
- return $this->date_structure;
- }
-
- if (empty($this->permalink_structure)) {
- $this->date_structure = '';
- return false;
- }
-
- // The date permalink must have year, month, and day separated by slashes.
- $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
-
- $this->date_structure = '';
- $date_endian = '';
-
- foreach ($endians as $endian) {
- if (false !== strpos($this->permalink_structure, $endian)) {
- $date_endian= $endian;
- break;
- }
- }
-
- if ( empty($date_endian) )
- $date_endian = '%year%/%monthnum%/%day%';
-
- // Do not allow the date tags and %post_id% to overlap in the permalink
- // structure. If they do, move the date tags to $front/date/.
- $front = $this->front;
- preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
- $tok_index = 1;
- foreach ($tokens[0] as $token) {
- if ( ($token == '%post_id%') && ($tok_index <= 3) ) {
- $front = $front . 'date/';
- break;
- }
- }
-
- $this->date_structure = $front . $date_endian;
-
- return $this->date_structure;
- }
-
- function get_year_permastruct() {
- $structure = $this->get_date_permastruct($this->permalink_structure);
-
- if (empty($structure)) {
- return false;
- }
-
- $structure = str_replace('%monthnum%', '', $structure);
- $structure = str_replace('%day%', '', $structure);
-
- $structure = preg_replace('#/+#', '/', $structure);
-
- return $structure;
- }
-
- function get_month_permastruct() {
- $structure = $this->get_date_permastruct($this->permalink_structure);
-
- if (empty($structure)) {
- return false;
- }
-
- $structure = str_replace('%day%', '', $structure);
-
- $structure = preg_replace('#/+#', '/', $structure);
-
- return $structure;
- }
-
- function get_day_permastruct() {
- return $this->get_date_permastruct($this->permalink_structure);
- }
-
- function get_category_permastruct() {
- if (isset($this->category_structure)) {
- return $this->category_structure;
- }
-
- if (empty($this->permalink_structure)) {
- $this->category_structure = '';
- return false;
- }
-
- if (empty($this->category_base))
- $this->category_structure = $this->front . 'tag/';
- else
- $this->category_structure = $this->category_base . '/';
-
- $this->category_structure .= '%category%';
-
- return $this->category_structure;
- }
-
- function get_author_permastruct() {
- if (isset($this->author_structure)) {
- return $this->author_structure;
- }
-
- if (empty($this->permalink_structure)) {
- $this->author_structure = '';
- return false;
- }
-
- $this->author_structure = $this->front . $this->author_base . '/%author%';
-
- return $this->author_structure;
- }
-
- function get_search_permastruct() {
- if (isset($this->search_structure)) {
- return $this->search_structure;
- }
-
- if (empty($this->permalink_structure)) {
- $this->search_structure = '';
- return false;
- }
-
- $this->search_structure = $this->root . $this->search_base . '/%search%';
-
- return $this->search_structure;
- }
-
- function get_page_permastruct() {
- if (isset($this->page_structure)) {
- return $this->page_structure;
- }
-
- if (empty($this->permalink_structure)) {
- $this->page_structure = '';
- return false;
- }
-
- $this->page_structure = $this->root . '%pagename%';
-
- return $this->page_structure;
- }
-
- function get_feed_permastruct() {
- if (isset($this->feed_structure)) {
- return $this->feed_structure;
- }
-
- if (empty($this->permalink_structure)) {
- $this->feed_structure = '';
- return false;
- }
-
- $this->feed_structure = $this->root . $this->feed_base . '/%feed%';
-
- return $this->feed_structure;
- }
-
- function get_comment_feed_permastruct() {
- if (isset($this->comment_feed_structure)) {
- return $this->comment_feed_structure;
- }
-
- if (empty($this->permalink_structure)) {
- $this->comment_feed_structure = '';
- return false;
- }
-
- $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
-
- return $this->comment_feed_structure;
- }
-
- function add_rewrite_tag($tag, $pattern, $query) {
- // If the tag already exists, replace the existing pattern and query for
- // that tag, otherwise add the new tag, pattern, and query to the end of
- // the arrays.
- $position = array_search($tag, $this->rewritecode);
- if (FALSE !== $position && NULL !== $position) {
- $this->rewritereplace[$position] = $pattern;
- $this->queryreplace[$position] = $query;
- } else {
- $this->rewritecode[] = $tag;
- $this->rewritereplace[] = $pattern;
- $this->queryreplace[] = $query;
- }
- }
-
- function generate_rewrite_rules($permalink_structure, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true) {
- $feedregex2 = '';
- foreach ($this->feeds as $feed_name) {
- $feedregex2 .= $feed_name . '|';
- }
- $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$';
- $feedregex = $this->feed_base . '/' . $feedregex2;
-
- $trackbackregex = 'trackback/?$';
- $pageregex = 'page/?([0-9]{1,})/?$';
-
- $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
- preg_match_all('/%.+?%/', $permalink_structure, $tokens);
-
- $num_tokens = count($tokens[0]);
-
- $index = $this->index;
- $feedindex = $index;
- $trackbackindex = $index;
- for ($i = 0; $i < $num_tokens; ++$i) {
- if (0 < $i) {
- $queries[$i] = $queries[$i - 1] . '&';
- }
-
- $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
- $queries[$i] .= $query_token;
- }
-
- $structure = $permalink_structure;
- if ($front != '/') {
- $structure = str_replace($front, '', $structure);
- }
- $structure = trim($structure, '/');
- if ($walk_dirs) {
- $dirs = explode('/', $structure);
- } else {
- $dirs[] = $structure;
- }
- $num_dirs = count($dirs);
-
- $front = preg_replace('|^/+|', '', $front);
-
- $post_rewrite = array();
- $struct = $front;
- for ($j = 0; $j < $num_dirs; ++$j) {
- $struct .= $dirs[$j] . '/';
- $struct = ltrim($struct, '/');
- $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
- $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
- $query = $queries[$num_toks - 1];
-
- $pagematch = $match . $pageregex;
- $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
-
- $feedmatch = $match . $feedregex;
- $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
-
- $feedmatch2 = $match . $feedregex2;
- $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
-
- if ($forcomments) {
- $feedquery .= '&withcomments=1';
- $feedquery2 .= '&withcomments=1';
- }
-
- $rewrite = array();
- if ($feed)
- $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2);
- if ($paged)
- $rewrite = array_merge($rewrite, array($pagematch => $pagequery));
-
- if ($num_toks) {
- $post = false;
- $page = false;
- if (strstr($struct, '%postname%') || strstr($struct, '%post_id%')
- || strstr($struct, '%pagename%')
- || (strstr($struct, '%year%') && strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) {
- $post = true;
- if ( strstr($struct, '%pagename%') )
- $page = true;
- $trackbackmatch = $match . $trackbackregex;
- $trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
- $match = rtrim($match, '/');
- $submatchbase = str_replace(array('(',')'),'',$match);
- $sub1 = $submatchbase . '/([^/]+)/';
- $sub1tb = $sub1 . $trackbackregex;
- $sub1feed = $sub1 . $feedregex;
- $sub1feed2 = $sub1 . $feedregex2;
- $sub1 .= '?$';
- $sub2 = $submatchbase . '/attachment/([^/]+)/';
- $sub2tb = $sub2 . $trackbackregex;
- $sub2feed = $sub2 . $feedregex;
- $sub2feed2 = $sub2 . $feedregex2;
- $sub2 .= '?$';
- $subquery = $index . '?attachment=' . $this->preg_index(1);
- $subtbquery = $subquery . '&tb=1';
- $subfeedquery = $subquery . '&feed=' . $this->preg_index(2);
- $match = $match . '(/[0-9]+)?/?$';
- $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
- } else {
- $match .= '?$';
- $query = $index . '?' . $query;
- }
-
- $rewrite = array_merge($rewrite, array($match => $query));
-
- if ($post) {
- $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);
- if ( ! $page )
- $rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery));
- $rewrite = array_merge($rewrite, array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery));
- }
- }
- $post_rewrite = array_merge($rewrite, $post_rewrite);
- }
- return $post_rewrite;
- }
-
- function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
- return $this->generate_rewrite_rules($permalink_structure, false, false, false, $walk_dirs);
- }
-
- /* rewrite_rules
- * Construct rewrite matches and queries from permalink structure.
- * Returns an associate array of matches and queries.
- */
- function rewrite_rules() {
- $rewrite = array();
-
- if (empty($this->permalink_structure)) {
- return $rewrite;
- }
-
- // Post
- $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure);
- $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite);
-
- // Date
- $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct());
- $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite);
-
- // Root
- $root_rewrite = $this->generate_rewrite_rules($this->root . '/');
- $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite);
-
- // Comments
- $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, true, true, true, false);
- $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite);
-
- // Search
- $search_structure = $this->get_search_permastruct();
- $search_rewrite = $this->generate_rewrite_rules($search_structure);
- $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite);
-
- // Categories
- $category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct());
- $category_rewrite = apply_filters('category_rewrite_rules', $category_rewrite);
-
- // Authors
- $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct());
- $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite);
-
- // Pages
- $page_rewrite = $this->page_rewrite_rules();
- $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
-
- // Put them together.
- $this->rules = array_merge($page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $author_rewrite, $date_rewrite, $post_rewrite);
-
- do_action('generate_rewrite_rules', array(&$this));
- $this->rules = apply_filters('rewrite_rules_array', $this->rules);
-
- return $this->rules;
- }
-
- function wp_rewrite_rules() {
- $this->rules = get_option('rewrite_rules');
- if ( empty($this->rules) ) {
- $this->matches = 'matches';
- $this->rewrite_rules();
- update_option('rewrite_rules', $this->rules);
- }
-
- return $this->rules;
- }
-
- function mod_rewrite_rules() {
- if ( ! $this->using_permalinks()) {
- return '';
- }
-
- $site_root = parse_url(get_settings('siteurl'));
- $site_root = trailingslashit($site_root['path']);
-
- $home_root = parse_url(get_settings('home'));
- $home_root = trailingslashit($home_root['path']);
-
- $rules = "<IfModule mod_rewrite.c>\n";
- $rules .= "RewriteEngine On\n";
- $rules .= "RewriteBase $home_root\n";
-
- if ($this->use_verbose_rules) {
- $this->matches = '';
- $rewrite = $this->rewrite_rules();
- $num_rules = count($rewrite);
- $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
- "RewriteCond %{REQUEST_FILENAME} -d\n" .
- "RewriteRule ^.*$ - [S=$num_rules]\n";
-
- foreach ($rewrite as $match => $query) {
- // Apache 1.3 does not support the reluctant (non-greedy) modifier.
- $match = str_replace('.+?', '.+', $match);
-
- // If the match is unanchored and greedy, prepend rewrite conditions
- // to avoid infinite redirects and eclipsing of real files.
- if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
- //nada.
- }
-
- if (strstr($query, $this->index)) {
- $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
- } else {
- $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
- }
- }
- } else {
- $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" .
- "RewriteCond %{REQUEST_FILENAME} !-d\n" .
- "RewriteRule . {$home_root}{$this->index} [L]\n";
- }
-
- $rules .= "</IfModule>\n";
-
- $rules = apply_filters('mod_rewrite_rules', $rules);
- $rules = apply_filters('rewrite_rules', $rules); // Deprecated
-
- return $rules;
- }
-
- function flush_rules() {
- generate_page_uri_index();
- delete_option('rewrite_rules');
- $this->wp_rewrite_rules();
- if ( function_exists('save_mod_rewrite_rules') )
- save_mod_rewrite_rules();
- }
-
- function init() {
- $this->permalink_structure = get_settings('permalink_structure');
- $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));
- $this->root = '';
- if ($this->using_index_permalinks()) {
- $this->root = $this->index . '/';
- }
- $this->category_base = get_settings('category_base');
- unset($this->category_structure);
- unset($this->author_structure);
- unset($this->date_structure);
- unset($this->page_structure);
- unset($this->search_structure);
- unset($this->feed_structure);
- unset($this->comment_feed_structure);
- }
-
- function set_permalink_structure($permalink_structure) {
- if ($permalink_structure != $this->permalink_structure) {
- update_option('permalink_structure', $permalink_structure);
- $this->init();
- }
- }
-
- function set_category_base($category_base) {
- if ($category_base != $this->category_base) {
- update_option('category_base', $category_base);
- $this->init();
- }
- }
-
- function WP_Rewrite() {
- $this->init();
- }
-}
-
class WP {
var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 's', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview');
@@ -1448,6 +84,10 @@ class WP {
var $matched_rule;
var $matched_query;
var $did_permalink = false;
+
+ function add_query_var($qv) {
+ $this->public_query_vars[] = $qv;
+ }
function parse_request($extra_query_vars = '') {
global $wp_rewrite;
@@ -1520,7 +160,7 @@ class WP {
preg_match("!^$match!", urldecode($request_match), $matches)) {
// Got a match.
$this->matched_rule = $match;
-
+
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\?!", '', $query);
@@ -1605,7 +245,8 @@ class WP {
@header("ETag: $wp_etag");
// Support for Conditional GET
- if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) $client_etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
+ if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
+ $client_etag = stripslashes(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
else $client_etag = false;
$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE']);
@@ -1663,7 +304,7 @@ class WP {
}
function init() {
- get_currentuserinfo();
+ wp_get_current_user();
}
function query_posts() {
@@ -1699,4 +340,343 @@ class WP {
}
}
+class WP_Error {
+ var $errors = array();
+
+ function WP_Error($code = '', $message = '') {
+ if ( ! empty($code) )
+ $this->errors[$code][] = $message;
+ }
+
+ function get_error_codes() {
+ if ( empty($this->errors) )
+ return array();
+
+ return array_keys($this->errors);
+ }
+
+ function get_error_code() {
+ $codes = $this->get_error_codes();
+
+ if ( empty($codes) )
+ return '';
+
+ return $codes[0];
+ }
+
+ function get_error_messages($code = '') {
+ // Return all messages if no code specified.
+ if ( empty($code) ) {
+ $all_messages = array();
+ foreach ( $this->errors as $code => $messages )
+ $all_messages = array_merge($all_messages, $messages);
+
+ return $all_messages;
+ }
+
+ if ( isset($this->errors[$code]) )
+ return $this->errors[$code];
+ else
+ return array();
+ }
+
+ function get_error_message($code = '') {
+ if ( empty($code) )
+ $code = $this->get_error_code();
+ $messages = $this->get_error_messages($code);
+ if ( empty($messages) )
+ return '';
+ return $messages[0];
+ }
+
+ function add($code, $message) {
+ $this->errors[$code][] = $message;
+ }
+}
+
+function is_wp_error($thing) {
+ if ( is_object($thing) && is_a($thing, 'WP_Error') )
+ return true;
+ return false;
+}
+
+
+// A class for displaying various tree-like structures. Extend the Walker class to use it, see examples at the bottom
+
+class Walker {
+ var $tree_type;
+ var $db_fields;
+
+ //abstract callbacks
+ function start_lvl($output) { return $output; }
+ function end_lvl($output) { return $output; }
+ function start_el($output) { return $output; }
+ function end_el($output) { return $output; }
+
+ function walk($elements, $to_depth) {
+ $args = array_slice(func_get_args(), 2); $parents = array(); $depth = 1; $previous_element = ''; $output = '';
+
+ //padding at the end
+ $last_element->post_parent = 0;
+ $last_element->post_id = 0;
+ $elements[] = $last_element;
+
+ $id_field = $this->db_fields['id'];
+ $parent_field = $this->db_fields['parent'];
+
+ $flat = ($to_depth == -1) ? true : false;
+
+ foreach ( $elements as $element ) {
+ // If flat, start and end the element and skip the level checks.
+ if ( $flat) {
+ // Start the element.
+ if ( $element->$id_field != 0 ) {
+ $cb_args = array_merge( array($output, $element, $depth - 1), $args);
+ $output = call_user_func_array(array(&$this, 'start_el'), $cb_args);
+ }
+
+ // End the element.
+ if ( $element->$id_field != 0 ) {
+ $cb_args = array_merge( array($output, $element, $depth - 1), $args);
+ $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
+ }
+
+ continue;
+ }
+
+ // Walk the tree.
+ if ( !empty($previous_element) && ($element->$parent_field == $previous_element->$id_field) ) {
+ // Previous element is my parent. Descend a level.
+ array_unshift($parents, $previous_element);
+ $depth++; //always do this so when we start the element further down, we know where we are
+ if ( !$to_depth || ($depth < $to_depth) ) { //only descend if we're below $to_depth
+ $cb_args = array_merge( array($output, $depth - 1), $args);
+ $output = call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
+ }
+ } else if ( $element->$parent_field == $previous_element->$parent_field) {
+ // On the same level as previous element.
+ if ( !$to_depth || ($depth <= $to_depth) ) {
+ $cb_args = array_merge( array($output, $previous_element, $depth - 1), $args);
+ $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
+ }
+ } else if ( $depth > 1 ) {
+ // Ascend one or more levels.
+ if ( !$to_depth || ($depth <= $to_depth) ) {
+ $cb_args = array_merge( array($output, $previous_element, $depth - 1), $args);
+ $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
+ }
+
+ while ( $parent = array_shift($parents) ) {
+ $depth--;
+ if ( !$to_depth || ($depth < $to_depth) ) {
+ $cb_args = array_merge( array($output, $depth - 1), $args);
+ $output = call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
+ $cb_args = array_merge( array($output, $parent, $depth - 1), $args);
+ $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
+ }
+ if ( $element->$parent_field == $parents[0]->$id_field ) {
+ break;
+ }
+ }
+ } else if ( !empty($previous_element) ) {
+ // Close off previous element.
+ if ( !$to_depth || ($depth <= $to_depth) ) {
+ $cb_args = array_merge( array($output, $previous_element, $depth - 1), $args);
+ $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
+ }
+ }
+
+ // Start the element.
+ if ( !$to_depth || ($depth <= $to_depth) ) {
+ if ( $element->$id_field != 0 ) {
+ $cb_args = array_merge( array($output, $element, $depth - 1), $args);
+ $output = call_user_func_array(array(&$this, 'start_el'), $cb_args);
+ }
+ }
+
+ $previous_element = $element;
+ }
+
+ return $output;
+ }
+}
+
+class Walker_Page extends Walker {
+ var $tree_type = 'page';
+ var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
+
+ function start_lvl($output, $depth) {
+ $indent = str_repeat("\t", $depth);
+ $output .= "$indent<ul>\n";
+ return $output;
+ }
+
+ function end_lvl($output, $depth) {
+ $indent = str_repeat("\t", $depth);
+ $output .= "$indent</ul>\n";
+ return $output;
+ }
+
+ function start_el($output, $page, $depth, $current_page, $show_date, $date_format) {
+ if ( $depth )
+ $indent = str_repeat("\t", $depth);
+
+ $css_class = 'page_item';
+ if ( $page->ID == $current_page )
+ $css_class .= ' current_page_item';
+
+ $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . wp_specialchars($page->post_title) . '">' . $page->post_title . '</a>';
+
+ if ( !empty($show_date) ) {
+ if ( 'modified' == $show_date )
+ $time = $page->post_modified;
+ else
+ $time = $page->post_date;
+
+ $output .= " " . mysql2date($date_format, $time);
+ }
+
+ return $output;
+ }
+
+ function end_el($output, $page, $depth) {
+ $output .= "</li>\n";
+
+ return $output;
+ }
+
+}
+
+class Walker_PageDropdown extends Walker {
+ var $tree_type = 'page';
+ var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
+
+ function start_el($output, $page, $depth, $args) {
+ $pad = str_repeat('&nbsp;', $depth * 3);
+
+ $output .= "\t<option value=\"$page->ID\"";
+ if ( $page->ID == $args['selected'] )
+ $output .= ' selected="selected"';
+ $output .= '>';
+ $title = wp_specialchars($page->post_title);
+ $output .= "$pad$title";
+ $output .= "</option>\n";
+
+ return $output;
+ }
+}
+
+class Walker_Category extends Walker {
+ var $tree_type = 'category';
+ var $db_fields = array ('parent' => 'category_parent', 'id' => 'cat_ID'); //TODO: decouple this
+
+ function start_lvl($output, $depth, $args) {
+ if ( 'list' != $args['style'] )
+ return $output;
+
+ $indent = str_repeat("\t", $depth);
+ $output .= "$indent<ul class='children'>\n";
+ return $output;
+ }
+
+ function end_lvl($output, $depth, $args) {
+ if ( 'list' != $args['style'] )
+ return $output;
+
+ $indent = str_repeat("\t", $depth);
+ $output .= "$indent</ul>\n";
+ return $output;
+ }
+
+ function start_el($output, $category, $depth, $args) {
+ extract($args);
+
+ $link = '<a href="' . get_category_link($category->cat_ID) . '" ';
+ if ( $use_desc_for_title == 0 || empty($category->category_description) )
+ $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"';
+ else
+ $link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"';
+ $link .= '>';
+ $link .= apply_filters('list_cats', $category->cat_name, $category).'</a>';
+
+ if ( (! empty($feed_image)) || (! empty($feed)) ) {
+ $link .= ' ';
+
+ if ( empty($feed_image) )
+ $link .= '(';
+
+ $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"';
+
+ if ( !empty($feed) ) {
+ $title = ' title="' . $feed . '"';
+ $alt = ' alt="' . $feed . '"';
+ $name = $feed;
+ $link .= $title;
+ }
+
+ $link .= '>';
+
+ if ( !empty($feed_image) )
+ $link .= "<img src='$feed_image' $alt$title" . ' />';
+ else
+ $link .= $name;
+ $link .= '</a>';
+ if (empty($feed_image))
+ $link .= ')';
+ }
+
+ if ( $show_count )
+ $link .= ' ('.intval($category->category_count).')';
+
+ if ( $show_date ) {
+ $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
+ }
+
+ if ( 'list' == $args['style'] ) {
+ $output .= "\t<li";
+ if ( ($category->cat_ID == $current_category) && is_category() )
+ $output .= ' class="current-cat"';
+ $output .= ">$link\n";
+ } else {
+ $output .= "\t$link<br />\n";
+ }
+
+ return $output;
+ }
+
+ function end_el($output, $page, $depth, $args) {
+ if ( 'list' != $args['style'] )
+ return $output;
+
+ $output .= "</li>\n";
+ return $output;
+ }
+
+}
+
+class Walker_CategoryDropdown extends Walker {
+ var $tree_type = 'category';
+ var $db_fields = array ('parent' => 'category_parent', 'id' => 'cat_ID'); //TODO: decouple this
+
+ function start_el($output, $category, $depth, $args) {
+ $pad = str_repeat('&nbsp;', $depth * 3);
+
+ $cat_name = apply_filters('list_cats', $category->cat_name, $category);
+ $output .= "\t<option value=\"".$category->cat_ID."\"";
+ if ( $category->cat_ID == $args['selected'] )
+ $output .= ' selected="selected"';
+ $output .= '>';
+ $output .= $cat_name;
+ if ( $args['show_count'] )
+ $output .= '&nbsp;&nbsp;('. $category->category_count .')';
+ if ( $args['show_last_update'] ) {
+ $format = 'Y-m-d';
+ $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
+ }
+ $output .= "</option>\n";
+
+ return $output;
+ }
+}
+
?>