Comment updating cleanup. Add get_comment(), edit_comment(), get_comment_to_edit(), and wp_update_comment().

git-svn-id: https://develop.svn.wordpress.org/trunk@2662 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2005-06-26 19:55:12 +00:00
parent a6881402ef
commit 706c897f32
5 changed files with 198 additions and 124 deletions

View File

@ -63,13 +63,13 @@ function edit_post() {
if ( !isset($blog_ID) ) if ( !isset($blog_ID) )
$blog_ID = 1; $blog_ID = 1;
$post_ID = $_POST['post_ID']; $post_ID = (int) $_POST['post_ID'];
if (!user_can_edit_post($user_ID, $post_ID, $blog_ID)) if (!user_can_edit_post($user_ID, $post_ID, $blog_ID))
die( __('You are not allowed to edit this post.') ); die( __('You are not allowed to edit this post.') );
// Rename. // Rename.
$_POST['ID'] = $_POST['post_ID']; $_POST['ID'] = (int) $_POST['post_ID'];
$_POST['post_content'] = $_POST['content']; $_POST['post_content'] = $_POST['content'];
$_POST['post_excerpt'] = $_POST['excerpt']; $_POST['post_excerpt'] = $_POST['excerpt'];
$_POST['post_parent'] = $_POST['parent_id']; $_POST['post_parent'] = $_POST['parent_id'];
@ -119,6 +119,39 @@ function edit_post() {
add_meta($post_ID); add_meta($post_ID);
} }
function edit_comment() {
global $user_ID;
$comment_ID = (int) $_POST['comment_ID'];
$comment_post_ID = (int) $_POST['comment_post_ID'];
if (!user_can_edit_post_comments($user_ID, $comment_post_ID))
die( __('You are not allowed to edit comments on this post, so you cannot edit this comment.') );
$_POST['comment_author'] = $_POST['newcomment_author'];
$_POST['comment_author_email'] = $_POST['newcomment_author_email'];
$_POST['comment_author_url'] = $_POST['newcomment_author_url'];
$_POST['comment_approved'] = $_POST['comment_status'];
$_POST['comment_content'] = $_POST['content'];
$_POST['comment_ID'] = (int) $_POST['comment_ID'];
if (user_can_edit_post_date($user_ID, $post_ID) && (!empty($_POST['edit_date']))) {
$aa = $_POST['aa'];
$mm = $_POST['mm'];
$jj = $_POST['jj'];
$hh = $_POST['hh'];
$mn = $_POST['mn'];
$ss = $_POST['ss'];
$jj = ($jj > 31) ? 31 : $jj;
$hh = ($hh > 23) ? $hh - 24 : $hh;
$mn = ($mn > 59) ? $mn - 60 : $mn;
$ss = ($ss > 59) ? $ss - 60 : $ss;
$_POST['comment_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
}
wp_update_comment($_POST);
}
// Get an existing post and format it for editing. // Get an existing post and format it for editing.
function get_post_to_edit($id) { function get_post_to_edit($id) {
$post = get_post($id); $post = get_post($id);
@ -158,6 +191,19 @@ function get_default_post_to_edit() {
return $post; return $post;
} }
function get_comment_to_edit($id) {
$comment = get_comment($id);
$comment->comment_content = format_to_edit($comment->comment_content);
$comment->comment_content = apply_filters('comment_edit_pre', $comment->comment_content);
$comment->comment_author = format_to_edit($comment->comment_author);
$comment->comment_author_email = format_to_edit($comment->comment_author_email);
$comment->comment_author_url = format_to_edit($comment->comment_author_url);
return $comment;
}
function url_shorten ($url) { function url_shorten ($url) {
$short_url = str_replace('http://', '', stripslashes($url)); $short_url = str_replace('http://', '', stripslashes($url));
$short_url = str_replace('www.', '', $short_url); $short_url = str_replace('www.', '', $short_url);

View File

@ -1,8 +1,8 @@
<?php <?php
$submitbutton_text = __('Edit Comment &raquo;'); $submitbutton_text = __('Edit Comment &raquo;');
$toprow_title = sprintf(__('Editing Comment # %s'), $commentdata['comment_ID']); $toprow_title = sprintf(__('Editing Comment # %s'), $comment->comment_ID);
$form_action = 'editedcomment'; $form_action = 'editedcomment';
$form_extra = "' />\n<input type='hidden' name='comment_ID' value='$comment' />\n<input type='hidden' name='comment_post_ID' value='".$commentdata["comment_post_ID"]; $form_extra = "' />\n<input type='hidden' name='comment_ID' value='" . $comment->comment_ID . "' />\n<input type='hidden' name='comment_post_ID' value='".$comment->comment_post_ID;
?> ?>
<form name="post" action="post.php" method="post" id="post"> <form name="post" action="post.php" method="post" id="post">
@ -20,19 +20,19 @@ window.onload = focusit;
<fieldset id="namediv"> <fieldset id="namediv">
<legend><?php _e('Name:') ?></legend> <legend><?php _e('Name:') ?></legend>
<div> <div>
<input type="text" name="newcomment_author" size="22" value="<?php echo format_to_edit($commentdata['comment_author']) ?>" tabindex="1" id="name" /> <input type="text" name="newcomment_author" size="22" value="<?php echo $comment->comment_author ?>" tabindex="1" id="name" />
</div> </div>
</fieldset> </fieldset>
<fieldset id="emaildiv"> <fieldset id="emaildiv">
<legend><?php _e('E-mail:') ?></legend> <legend><?php _e('E-mail:') ?></legend>
<div> <div>
<input type="text" name="newcomment_author_email" size="30" value="<?php echo format_to_edit($commentdata['comment_author_email']) ?>" tabindex="2" id="email" /> <input type="text" name="newcomment_author_email" size="30" value="<?php echo $comment->comment_author_email ?>" tabindex="2" id="email" />
</div> </div>
</fieldset> </fieldset>
<fieldset id="uridiv"> <fieldset id="uridiv">
<legend><?php _e('URI:') ?></legend> <legend><?php _e('URI:') ?></legend>
<div> <div>
<input type="text" name="newcomment_author_url" size="35" value="<?php echo format_to_edit($commentdata['comment_author_url']) ?>" tabindex="3" id="URL" /> <input type="text" name="newcomment_author_url" size="35" value="<?php echo $comment->comment_author_url ?>" tabindex="3" id="URL" />
</div> </div>
</fieldset> </fieldset>
@ -45,7 +45,7 @@ window.onload = focusit;
$rows = 10; $rows = 10;
} }
?> ?>
<div><textarea rows="<?php echo $rows; ?>" cols="40" name="content" tabindex="4" id="content" style="width: 99%"><?php echo $content ?></textarea></div> <div><textarea rows="<?php echo $rows; ?>" cols="40" name="content" tabindex="4" id="content" style="width: 99%"><?php echo $comment->comment_content ?></textarea></div>
</fieldset> </fieldset>
<script type="text/javascript"> <script type="text/javascript">
@ -66,9 +66,9 @@ edCanvas = document.getElementById('content');
<table width="100%" cellspacing="2" cellpadding="5" class="editform"> <table width="100%" cellspacing="2" cellpadding="5" class="editform">
<tr> <tr>
<th scope="row" valign="top"><?php _e('Comment Status') ?>:</th> <th scope="row" valign="top"><?php _e('Comment Status') ?>:</th>
<td><label for="comment_status_approved" class="selectit"><input id="comment_status_approved" name="comment_status" type="radio" value="1" <?php checked($comment_status, '1'); ?> /> <?php _e('Approved') ?></label><br /> <td><label for="comment_status_approved" class="selectit"><input id="comment_status_approved" name="comment_status" type="radio" value="1" <?php checked($comment->comment_approved, '1'); ?> /> <?php _e('Approved') ?></label><br />
<label for="comment_status_moderated" class="selectit"><input id="comment_status_moderated" name="comment_status" type="radio" value="0" <?php checked($comment_status, '0'); ?> /> <?php _e('Moderated') ?></label><br /> <label for="comment_status_moderated" class="selectit"><input id="comment_status_moderated" name="comment_status" type="radio" value="0" <?php checked($comment->comment_approved, '0'); ?> /> <?php _e('Moderated') ?></label><br />
<label for="comment_status_spam" class="selectit"><input id="comment_status_spam" name="comment_status" type="radio" value="spam" <?php checked($comment_status, 'spam'); ?> /> <?php _e('Spam') ?></label></td> <label for="comment_status_spam" class="selectit"><input id="comment_status_spam" name="comment_status" type="radio" value="spam" <?php checked($comment->comment_approved, 'spam'); ?> /> <?php _e('Spam') ?></label></td>
</tr> </tr>
<?php if ($user_level > 4) : ?> <?php if ($user_level > 4) : ?>
@ -80,7 +80,7 @@ edCanvas = document.getElementById('content');
<tr> <tr>
<th scope="row"><?php _e('Delete'); ?>:</th> <th scope="row"><?php _e('Delete'); ?>:</th>
<td><p><a class="delete" href="post.php?action=confirmdeletecomment&amp;noredir=true&amp;comment=<?php echo $commentdata['comment_ID']; ?>&amp;p=<?php echo $commentdata['comment_post_ID']; ?>"><?php _e('Delete comment') ?></a></p></td> <td><p><a class="delete" href="post.php?action=confirmdeletecomment&amp;noredir=true&amp;comment=<?php echo $comment->comment_ID; ?>&amp;p=<?php echo $comment->comment_post_ID; ?>"><?php _e('Delete comment') ?></a></p></td>
</tr> </tr>
</table> </table>

View File

@ -140,18 +140,15 @@ case 'editcomment':
get_currentuserinfo(); get_currentuserinfo();
$comment = $_GET['comment']; $comment = (int) $_GET['comment'];
$commentdata = get_commentdata($comment, 1, true) or die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'javascript:history.go(-1)'));
if (!user_can_edit_post_comments($user_ID, $commentdata['comment_post_ID'])) { if ( ! $comment = get_comment($comment) )
die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'javascript:history.go(-1)'));
if ( !user_can_edit_post_comments($user_ID, $comment->comment_post_ID) )
die( __('You are not allowed to edit comments on this post.') ); die( __('You are not allowed to edit comments on this post.') );
}
$content = $commentdata['comment_content']; $comment = get_comment_to_edit($comment);
$content = format_to_edit($content);
$content = apply_filters('comment_edit_pre', $content);
$comment_status = $commentdata['comment_approved'];
include('edit-form-comment.php'); include('edit-form-comment.php');
@ -161,21 +158,22 @@ case 'confirmdeletecomment':
require_once('./admin-header.php'); require_once('./admin-header.php');
$comment = $_GET['comment']; $comment = (int) $_GET['comment'];
$p = (int) $_GET['p']; $p = (int) $_GET['p'];
$commentdata = get_commentdata($comment, 1, true) or die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'edit.php'));
if (!user_can_delete_post_comments($user_ID, $commentdata['comment_post_ID'])) { if ( ! $comment = get_comment($comment) )
die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'edit.php'));
if ( !user_can_delete_post_comments($user_ID, $comment->comment_post_ID) )
die( __('You are not allowed to delete comments on this post.') ); die( __('You are not allowed to delete comments on this post.') );
}
echo "<div class=\"wrap\">\n"; echo "<div class=\"wrap\">\n";
echo "<p>" . __('<strong>Caution:</strong> You are about to delete the following comment:') . "</p>\n"; echo "<p>" . __('<strong>Caution:</strong> You are about to delete the following comment:') . "</p>\n";
echo "<table border=\"0\">\n"; echo "<table border=\"0\">\n";
echo "<tr><td>" . __('Author:') . "</td><td>" . $commentdata["comment_author"] . "</td></tr>\n"; echo "<tr><td>" . __('Author:') . "</td><td>" . $comment->comment_author . "</td></tr>\n";
echo "<tr><td>" . __('E-mail:') . "</td><td>" . $commentdata["comment_author_email"] . "</td></tr>\n"; echo "<tr><td>" . __('E-mail:') . "</td><td>" . $comment->comment_author_email . "</td></tr>\n";
echo "<tr><td>". __('URL:') . "</td><td>" . $commentdata["comment_author_url"] . "</td></tr>\n"; echo "<tr><td>". __('URL:') . "</td><td>" . $comment->comment_author_url . "</td></tr>\n";
echo "<tr><td>". __('Comment:') . "</td><td>" . stripslashes($commentdata["comment_content"]) . "</td></tr>\n"; echo "<tr><td>". __('Comment:') . "</td><td>" . $comment->comment_content . "</td></tr>\n";
echo "</table>\n"; echo "</table>\n";
echo "<p>" . __('Are you sure you want to do that?') . "</p>\n"; echo "<p>" . __('Are you sure you want to do that?') . "</p>\n";
@ -196,8 +194,8 @@ case 'deletecomment':
check_admin_referer(); check_admin_referer();
$comment = $_GET['comment']; $comment = (int) $_GET['comment'];
$p = $_GET['p']; $p = (int) $_GET['p'];
if (isset($_GET['noredir'])) { if (isset($_GET['noredir'])) {
$noredir = true; $noredir = true;
} else { } else {
@ -205,14 +203,15 @@ case 'deletecomment':
} }
$postdata = get_post($p) or die(sprintf(__('Oops, no post with this ID. <a href="%s">Go back</a>!'), 'edit.php')); $postdata = get_post($p) or die(sprintf(__('Oops, no post with this ID. <a href="%s">Go back</a>!'), 'edit.php'));
$commentdata = get_commentdata($comment, 1, true) or die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'post.php'));
if (!user_can_delete_post_comments($user_ID, $commentdata['comment_post_ID'])) { if ( ! $comment = get_comment($comment) )
die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'post.php'));
if (!user_can_delete_post_comments($user_ID, $commentdata['comment_post_ID']))
die( __('You are not allowed to edit comments on this post.') ); die( __('You are not allowed to edit comments on this post.') );
}
wp_set_comment_status($comment, "delete"); wp_set_comment_status($comment->comment_ID, "delete");
do_action('delete_comment', $comment); do_action('delete_comment', $comment->comment_ID);
if (($_SERVER['HTTP_REFERER'] != "") && (false == $noredir)) { if (($_SERVER['HTTP_REFERER'] != "") && (false == $noredir)) {
header('Location: ' . $_SERVER['HTTP_REFERER']); header('Location: ' . $_SERVER['HTTP_REFERER']);
@ -228,21 +227,21 @@ case 'unapprovecomment':
check_admin_referer(); check_admin_referer();
$comment = $_GET['comment']; $comment = (int) $_GET['comment'];
$p = $_GET['p']; $p = (int) $_GET['p'];
if (isset($_GET['noredir'])) { if (isset($_GET['noredir'])) {
$noredir = true; $noredir = true;
} else { } else {
$noredir = false; $noredir = false;
} }
$commentdata = get_commentdata($comment) or die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'edit.php')); if ( ! $comment = get_comment($comment) )
die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'edit.php'));
if (!user_can_edit_post_comments($user_ID, $commentdata['comment_post_ID'])) { if ( !user_can_edit_post_comments($user_ID, $comment->comment_post_ID) )
die( __('You are not allowed to edit comments on this post, so you cannot disapprove this comment.') ); die( __('You are not allowed to edit comments on this post, so you cannot disapprove this comment.') );
}
wp_set_comment_status($comment, "hold"); wp_set_comment_status($comment->comment_ID, "hold");
if (($_SERVER['HTTP_REFERER'] != "") && (false == $noredir)) { if (($_SERVER['HTTP_REFERER'] != "") && (false == $noredir)) {
header('Location: ' . $_SERVER['HTTP_REFERER']); header('Location: ' . $_SERVER['HTTP_REFERER']);
@ -256,16 +255,16 @@ case 'mailapprovecomment':
$comment = (int) $_GET['comment']; $comment = (int) $_GET['comment'];
$commentdata = get_commentdata($comment, 1, true) or die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'edit.php')); if ( ! $comment = get_comment($comment) )
die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'edit.php'));
if (!user_can_edit_post_comments($user_ID, $commentdata['comment_post_ID'])) { if ( !user_can_edit_post_comments($user_ID, $comment->comment_post_ID) )
die( __('You are not allowed to edit comments on this post, so you cannot approve this comment.') ); die( __('You are not allowed to edit comments on this post, so you cannot approve this comment.') );
}
if ('1' != $commentdata['comment_approved']) { if ('1' != $comment->comment_approved) {
wp_set_comment_status($comment, 'approve'); wp_set_comment_status($comment->comment_ID, 'approve');
if (true == get_option('comments_notify')) if (true == get_option('comments_notify'))
wp_notify_postauthor($comment); wp_notify_postauthor($comment->comment_ID);
} }
header('Location: ' . get_option('siteurl') . '/wp-admin/moderation.php?approved=1'); header('Location: ' . get_option('siteurl') . '/wp-admin/moderation.php?approved=1');
@ -274,22 +273,23 @@ case 'mailapprovecomment':
case 'approvecomment': case 'approvecomment':
$comment = $_GET['comment']; $comment = (int) $_GET['comment'];
$p = $_GET['p']; $p = (int) $_GET['p'];
if (isset($_GET['noredir'])) { if (isset($_GET['noredir'])) {
$noredir = true; $noredir = true;
} else { } else {
$noredir = false; $noredir = false;
} }
$commentdata = get_commentdata($comment) or die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'edit.php'));
if (!user_can_edit_post_comments($user_ID, $commentdata['comment_post_ID'])) { if ( ! $comment = get_comment($comment) )
die(sprintf(__('Oops, no comment with this ID. <a href="%s">Go back</a>!'), 'edit.php'));
if ( !user_can_edit_post_comments($user_ID, $commentdata['comment_post_ID']))
die( __('You are not allowed to edit comments on this post, so you cannot approve this comment.') ); die( __('You are not allowed to edit comments on this post, so you cannot approve this comment.') );
}
wp_set_comment_status($comment, "approve"); wp_set_comment_status($comment->comment_ID, "approve");
if (get_settings("comments_notify") == true) { if (get_settings("comments_notify") == true) {
wp_notify_postauthor($comment); wp_notify_postauthor($comment->comment_ID);
} }
@ -303,43 +303,7 @@ case 'approvecomment':
case 'editedcomment': case 'editedcomment':
$comment_ID = $_POST['comment_ID']; edit_comment();
$comment_post_ID = $_POST['comment_post_ID'];
$newcomment_author = $_POST['newcomment_author'];
$newcomment_author_email = $_POST['newcomment_author_email'];
$newcomment_author_url = $_POST['newcomment_author_url'];
$comment_status = $_POST['comment_status'];
if (!user_can_edit_post_comments($user_ID, $comment_post_ID)) {
die( __('You are not allowed to edit comments on this post, so you cannot edit this comment.') );
}
if (user_can_edit_post_date($user_ID, $post_ID) && (!empty($_POST['edit_date']))) {
$aa = $_POST['aa'];
$mm = $_POST['mm'];
$jj = $_POST['jj'];
$hh = $_POST['hh'];
$mn = $_POST['mn'];
$ss = $_POST['ss'];
$jj = ($jj > 31) ? 31 : $jj;
$hh = ($hh > 23) ? $hh - 24 : $hh;
$mn = ($mn > 59) ? $mn - 60 : $mn;
$ss = ($ss > 59) ? $ss - 60 : $ss;
$datemodif = ", comment_date = '$aa-$mm-$jj $hh:$mn:$ss'";
} else {
$datemodif = '';
}
$content = apply_filters('comment_save_pre', $_POST['content']);
$result = $wpdb->query("
UPDATE $wpdb->comments SET
comment_content = '$content',
comment_author = '$newcomment_author',
comment_author_email = '$newcomment_author_email',
comment_approved = '$comment_status',
comment_author_url = '$newcomment_author_url'".$datemodif."
WHERE comment_ID = $comment_ID"
);
$referredby = $_POST['referredby']; $referredby = $_POST['referredby'];
if (!empty($referredby)) { if (!empty($referredby)) {
@ -347,7 +311,7 @@ case 'editedcomment':
} else { } else {
header ("Location: edit.php?p=$comment_post_ID&c=1#comments"); header ("Location: edit.php?p=$comment_post_ID&c=1#comments");
} }
do_action('edit_comment', $comment_ID);
break; break;
default: default:

View File

@ -162,8 +162,7 @@ function wp_insert_post($postarr = array()) {
add_post_meta($post_ID, '_wp_page_template', $page_template, true); add_post_meta($post_ID, '_wp_page_template', $page_template, true);
} }
// Return insert_id if we got a good result, otherwise return zero. return $rval;
return $result ? $post_ID : 0;
} }
function wp_get_single_post($postid = 0, $mode = OBJECT) { function wp_get_single_post($postid = 0, $mode = OBJECT) {
@ -588,6 +587,41 @@ function wp_new_comment( $commentdata, $spam = false ) {
return $result; return $result;
} }
function wp_update_comment($commentarr) {
global $wpdb;
// First, get all of the original fields
$comment = get_comment($commentarr['comment_ID'], ARRAY_A);
// Escape data pulled from DB.
foreach ($comment as $key => $value)
$comment[$key] = $wpdb->escape($value);
// Merge old and new fields with new fields overwriting old ones.
$commentarr = array_merge($comment, $commentarr);
// Now extract the merged array.
extract($commentarr);
$comment_content = apply_filters('comment_save_pre', $comment_content);
$result = $wpdb->query(
"UPDATE $wpdb->comments SET
comment_content = '$comment_content',
comment_author = '$comment_author',
comment_author_email = '$comment_author_email',
comment_approved = '$comment_approved',
comment_author_url = '$comment_author_url',
comment_date = '$comment_date'
WHERE comment_ID = $comment_ID" );
$rval = $wpdb->rows_affected;
do_action('edit_comment', $comment_ID);
return $rval;
}
function do_trackbacks($post_id) { function do_trackbacks($post_id) {
global $wpdb; global $wpdb;

View File

@ -470,31 +470,31 @@ function &get_post(&$post, $output = OBJECT) {
if ( empty($post) ) { if ( empty($post) ) {
if ( isset($GLOBALS['post']) ) if ( isset($GLOBALS['post']) )
$post = & $GLOBALS['post']; $_post = & $GLOBALS['post'];
else else
$post = null; $_post = null;
} elseif (is_object($post) ) { } elseif (is_object($post) ) {
if (! isset($post_cache[$post->ID])) if (! isset($post_cache[$post->ID]))
$post_cache[$post->ID] = &$post; $post_cache[$post->ID] = &$post;
$post = & $post_cache[$post->ID]; $_post = & $post_cache[$post->ID];
} else { } else {
if (isset($post_cache[$post])) if (isset($post_cache[$post]))
$post = & $post_cache[$post]; $_post = & $post_cache[$post];
else { else {
$query = "SELECT * FROM $wpdb->posts WHERE ID=$post"; $query = "SELECT * FROM $wpdb->posts WHERE ID=$post";
$post_cache[$post] = & $wpdb->get_row($query); $post_cache[$post] = & $wpdb->get_row($query);
$post = & $post_cache[$post]; $_post = & $post_cache[$post];
} }
} }
if ( $output == OBJECT ) { if ( $output == OBJECT ) {
return $post; return $_post;
} elseif ( $output == ARRAY_A ) { } elseif ( $output == ARRAY_A ) {
return get_object_vars($post); return get_object_vars($_post);
} elseif ( $output == ARRAY_N ) { } elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($post)); return array_values(get_object_vars($_post));
} else { } else {
return $post; return $_post;
} }
} }
@ -505,33 +505,33 @@ function &get_page(&$page, $output = OBJECT) {
if ( empty($page) ) { if ( empty($page) ) {
if ( isset($GLOBALS['page']) ) if ( isset($GLOBALS['page']) )
$page = & $GLOBALS['page']; $_page = & $GLOBALS['page'];
else else
$page = null; $_page = null;
} elseif (is_object($page) ) { } elseif (is_object($page) ) {
if (! isset($page_cache[$page->ID])) if (! isset($page_cache[$page->ID]))
$page_cache[$page->ID] = &$page; $page_cache[$page->ID] = &$page;
$page = & $page_cache[$page->ID]; $_page = & $page_cache[$page->ID];
} else { } else {
if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) ) if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) )
$page = & $GLOBALS['page']; $_page = & $GLOBALS['page'];
elseif (isset($page_cache[$page])) elseif (isset($page_cache[$page]))
$page = & $page_cache[$page]; $_page = & $page_cache[$page];
else { else {
$query = "SELECT * FROM $wpdb->posts WHERE ID=$page"; $query = "SELECT * FROM $wpdb->posts WHERE ID=$page";
$page_cache[$page] = & $wpdb->get_row($query); $page_cache[$page] = & $wpdb->get_row($query);
$page = & $page_cache[$page]; $_page = & $page_cache[$page];
} }
} }
if ( $output == OBJECT ) { if ( $output == OBJECT ) {
return $page; return $_page;
} elseif ( $output == ARRAY_A ) { } elseif ( $output == ARRAY_A ) {
return get_object_vars($page); return get_object_vars($_page);
} elseif ( $output == ARRAY_N ) { } elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($page)); return array_values(get_object_vars($_page));
} else { } else {
return $page; return $_page;
} }
} }
@ -543,32 +543,62 @@ function &get_category(&$category, $output = OBJECT) {
if ( empty($category) ) if ( empty($category) )
return null; return null;
$category = (int) $category;
if ( ! isset($cache_categories)) if ( ! isset($cache_categories))
update_category_cache(); update_category_cache();
if (is_object($category)) { if (is_object($category)) {
if ( ! isset($cache_categories[$category->cat_ID])) if ( ! isset($cache_categories[$category->cat_ID]))
$cache_categories[$category->cat_ID] = &$category; $cache_categories[$category->cat_ID] = &$category;
$category = & $cache_categories[$category->cat_ID]; $_category = & $cache_categories[$category->cat_ID];
} else { } else {
if ( !isset($cache_categories[$category]) ) { if ( !isset($cache_categories[$category]) ) {
$category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = $category"); $_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = $category");
$cache_categories[$category->cat_ID] = & $category; $cache_categories[$category->cat_ID] = & $_category;
} else { } else {
$category = & $cache_categories[$category]; $_category = & $cache_categories[$category];
} }
} }
if ( $output == OBJECT ) { if ( $output == OBJECT ) {
return $category; return $_category;
} elseif ( $output == ARRAY_A ) { } elseif ( $output == ARRAY_A ) {
return get_object_vars($category); return get_object_vars($_category);
} elseif ( $output == ARRAY_N ) { } elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($category)); return array_values(get_object_vars($_category));
} else { } else {
return $category; return $_category;
}
}
// Retrieves comment data given a comment ID or comment object.
// Handles comment caching.
function &get_comment(&$comment, $output = OBJECT) {
global $comment_cache, $wpdb;
if ( empty($comment) )
return null;
if (is_object($comment)) {
if ( ! isset($comment_cache[$comment->comment_ID]))
$comment_cache[$comment->comment_ID] = &$comment;
$_comment = & $comment_cache[$comment->comment_ID];
} else {
if ( !isset($comment_cache[$comment]) ) {
$_comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment'");
$comment_cache[$comment->comment_ID] = & $_comment;
} else {
$_comment = & $comment_cache[$comment];
}
}
if ( $output == OBJECT ) {
return $_comment;
} elseif ( $output == ARRAY_A ) {
return get_object_vars($_comment);
} elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($_comment));
} else {
return $_comment;
} }
} }