Skip to content

Commit

Permalink
Finish comment listing code/style
Browse files Browse the repository at this point in the history
Comment form is now a dialog with AJAX post support
  • Loading branch information
claco committed Feb 8, 2009
1 parent d0497b2 commit 5f56a8f
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 29 deletions.
50 changes: 50 additions & 0 deletions comment-form.php
@@ -0,0 +1,50 @@
<?php if ('open' == $post->comment_status) : ?>

<div id="respond">

<h3 class="title"><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>

<div class="cancel-comment-reply">
<small><?php cancel_comment_reply_link(); ?></small>
</div>

<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">logged in</a> to post a comment.</p>
<?php else : ?>

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

<?php if ( $user_ID ) : ?>

<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out &raquo;</a></p>

<?php else : ?>

<div class="status"></div>

<p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" />
<label for="url"><small>Website</small></label></p>

<?php endif; ?>

<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->

<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>

<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
<?php comment_id_fields(); ?>
</p>
<?php do_action('comment_form', $post->ID); ?>

</form>

<?php endif; // If registration required and not logged in ?>
</div>

<?php endif; // if you delete this the sky will fall on your head ?>
3 changes: 1 addition & 2 deletions comment.php
Expand Up @@ -7,8 +7,7 @@
<?php endif; ?>
</div>
<div class="posted">
<?php comment_date(); ?>
<?php comment_time(); ?>
On <?php comment_date(); ?> <?php comment_time(); ?>, <?php comment_author_link(); ?> wrote:
</div>
<div class="content">
<?php comment_text(); ?>
Expand Down
84 changes: 84 additions & 0 deletions comments-ajax.php
@@ -0,0 +1,84 @@
<?php
require_once('../../../wp-config.php');

global $comment, $comments, $post, $wpdb, $user_ID, $user_identity, $user_email, $user_url;

function fail($s) {
header('HTTP/1.0 500 Internal Server Error');
echo $s;
exit;
}

foreach($_POST as $k=>$v) {
$_POST[$k] = urldecode($v);
}

$comment_post_ID = (int) $_POST['comment_post_ID'];

$post_status = $wpdb->get_var("SELECT comment_status FROM $wpdb->posts WHERE ID = '$comment_post_ID'");

if ( empty($post_status) ) {
do_action('comment_id_not_found', $comment_post_ID);
fail('The post you are trying to comment on does not curently exist in the database.');
} elseif ( 'closed' == $post_status ) {
do_action('comment_closed', $comment_post_ID);
fail(__('Sorry, comments are closed for this item.'));
}

$comment_author = trim($_POST['author']);
$comment_author_email = trim($_POST['email']);
$comment_author_url = trim($_POST['url']);
$comment_content = trim($_POST['comment']);

// If the user is logged in
get_currentuserinfo();
if ( $user_ID ) :
$comment_author = addslashes($user_identity);
$comment_author_email = addslashes($user_email);
$comment_author_url = addslashes($user_url);
else :
if ( get_option('comment_registration') )
fail(__('Sorry, you must be logged in to post a comment.'));
endif;

$comment_type = '';

if ( get_settings('require_name_email') && !$user_ID ) {
if ( 6 > strlen($comment_author_email) || '' == $comment_author )
fail(__('Error: please fill the required fields (name, email).'));
elseif ( !is_email($comment_author_email))
fail(__('Error: please enter a valid email address.'));
}

if ( '' == $comment_content )
fail(__('Error: please type a comment.'));

$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
if ( $comment_author_email )
$dupe .= "OR comment_author_email = '$comment_author_email' ";
$dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
if ( $wpdb->get_var($dupe) )
fail( __('Duplicate comment detected; it looks as though you\'ve already said that!') );

$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID');

$new_comment_ID = wp_new_comment($commentdata);

if ( !$user_ID ) :
setcookie('comment_author_' . COOKIEHASH, stripslashes($comment_author), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
setcookie('comment_author_email_' . COOKIEHASH, stripslashes($comment_author_email), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
setcookie('comment_author_url_' . COOKIEHASH, stripslashes($comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN);
endif;

$comment = $wpdb->get_row("SELECT * FROM {$wpdb->comments} WHERE comment_ID = " . $new_comment_ID);

$post->comment_status = $wpdb->get_var("SELECT comment_status FROM {$wpdb->posts} WHERE ID = {$comment_post_ID}");

ob_start();
$comments = array($comment);
include(TEMPLATEPATH . '/comments.php');
$commentout = ob_get_clean();
preg_match('#<li(.*?)>(.*)</li>#ims', $commentout, $matches);
echo "<li style=\"display:none\"".$matches[1].">".$matches[2]."</li>";

?>
32 changes: 12 additions & 20 deletions comments.php
@@ -1,23 +1,15 @@
<?php if ( have_comments() ) : ?>
<a name="comments"></a>
<div class="comments">
<h3 class="title">Comments</h3>
<?php while(have_comments()):the_comment();?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php include('comment.php');?>
<?php endwhile;?>
<h3 class="title">Comments (<a id="comment-add" href="#commentsform">Add</a>)</h3>
<?php if ( have_comments() ) : ?>
<?php while(have_comments()):the_comment();?>
<?php include('comment.php');?>
<?php endwhile;?>
<?php else: ?>
<p>There are no comments yet!</p>
<?php endif; ?>
</div>
<a name="commentsform"></a>
<div id="comment-form" class="comment-form">
<?php include('comment-form.php'); ?>
</div>
<?php endif; ?>
39 changes: 35 additions & 4 deletions css/style.css
Expand Up @@ -106,18 +106,22 @@ blockquote {
#content .entry .more-link:hover {
color: #83b728;
}
#content .comments .title {
font-size: 14px;
#content .comments .title, #content .comment-form .title {
font-size: 13px;
margin-bottom: 10px;
}
.comment {
padding: 11px 6px 2px 12px;
}
.comment.odd {
basckground-color: #efefef;
background-color: #efefef;
}
.comment .number {
font-size: 20px;
color: #bbbbbb;
font-family: Georgia;
font-weight: bold;
width: 25px;
width: 30px;
float: left;
}
.comment .author {
Expand All @@ -133,10 +137,35 @@ blockquote {
padding-left: 90px;
margin-bottom: 5px;
font-weight: bold;
font-style: italic;
font-size: 10px;
}
.comment .content {
padding-left: 90px;
}
.comment-form {
background-color: #ffffff;
}
.ui-dialog.comment-form {
padding: 20px;
}
.ui-dialog-titlebar-close {
float: right;
}
#comment-form .status {
color: #ff0000;
font-size: 12px;
font-style: italic;
display: none;
margin-bottom: 10px;
}
#comment-form input {
width: 200px;
margin-right: 15px;
}
#comment-form textarea {
width: 450px;
}


#recent {
Expand Down Expand Up @@ -169,7 +198,9 @@ ul.tags {
font-size: 10px;
font-weight: bold;
color: #000000;
background: #efefef;
display: none;
padding: 2px 5px 2px 5px;
}
ul.tags li {
display: inline;
Expand Down
1 change: 1 addition & 0 deletions header.php
Expand Up @@ -13,6 +13,7 @@
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jcarousellite.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.corner.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/external/syntax-highlighter/scripts/shCore.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/external/syntax-highlighter/scripts/shBrushXml.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/site.js"></script>
Expand Down

0 comments on commit 5f56a8f

Please sign in to comment.