- 2009-05-04 (月) 10:35
- WordPress
以前にこのブログで wp.vicuna.CMS. というテーマの入れ子への対応方法をご紹介しました。その後、wp.vicuna. のテーマのバージョンアップに合わせて、より設定が簡単にできるwp.vicuna.CMS.Ext.Customというツールを見つけて適応させました。このCustomのコードに合わせて入れ子方式に力業で対応させたんですが、このときに一部コードが抜けていたため酷いことになってました。…そう、少し前にこのブログで問題にしていた、承認前でもコメントが表示されてしまう件に関するコードです。
…ということでこの wp.vicuna.CMS.Ext.Custom にしたときの入れ子方式にするコードをご紹介しておきます。(もちろん自分への備忘録もかねて…)
まずはコメントを独自の表示形式で表示させるためのソースコード。これはwp.vecuna.exp.フォルダ直下のfunction.phpの末端に追加するだけでOKです。なおコメントが承認されているか否かを判定しているのは if('1' == $comment->comment_approved) です。
function vicuna_comments($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
if('1' == $comment->comment_approved){ ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div class="comment_ID" id="comment<?php comment_ID() ?>">
<span class="name"><?php comment_author_link() ?></span>
<span class="date"><?php comment_date(__('y-m-d (D) G:i', 'vicuna')) ?></span>
<?php edit_comment_link(__('Edit', 'vicuna'), '<span class="admin">', '</span>'); ?>
<span class="reply">
<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</span>
</div>
<div class="comment_text">
<?php comment_text() ?>
<?php
} else if('0' == $comment->comment_approved){
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div class="comment_ID" id="comment<?php comment_ID() ?>">
<span class="name">******</span>
<span class="date"><?php comment_date(__('y-m-d (D) G:i', 'vicuna')) ?></span>
<?php edit_comment_link(__('Edit', 'vicuna'), '<span class="admin">', '</span>'); ?>
<span class="reply">
<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</span>
</div>
<div class="comment_text">
<?php
_e('This comment is awaiting the approval of the author','vicuna');
?>
</div>
<?php
}
}
そして実際にコメントを表示するためのcomment.phpに対しても一部改変。こちらは承認されていない場合のコメント表示に関するコードが記述されているので、入れ子方式を選択しなかった場合に使用するコード内に含めるようにしています。
if (have_comments() || 'open' == $post->comment_status) : ?>
<div class="section" id="comments">
<h2><?php if ('open' == $post->comment_status) : _e('Comments', 'vicuna'); else : _e('Comments (Close)', 'vicuna'); endif; ?>:<span class="count"><?php echo $commentCount ?></span></h2>
<?php if (have_comments()) :
vicuna_edit_comments_link(__('Edit this comments.', 'vicuna'), '<p class="admin">', '</p>'); ?>
<?php if( function_exists( 'wp_list_comments' ) &amp;&amp; get_option('thread_comments') ){ ?>
<ol class="log">
<?php wp_list_comments( array( 'type' => 'comment', 'callback' => 'vicuna_comments' ) ); // 20090424 kny Edit ?>
</ol>
<?php }else{ ?>
<dl class="log">
<?php foreach ($commentArray as $comment) : ?>
<?php
/* -- whisper add s -- */
if('1' == $comment->comment_approved){
/* -- whisper add e -- */
?>
<dt id="comment<?php comment_ID() ?>"><span class="name"><?php comment_author_link() ?></span> <span class="date"><?php comment_date(__('y-m-d (D) G:i', 'vicuna')) ?></span> <?php edit_comment_link(__('Edit', 'vicuna'), '<span class="admin">', '</span>'); ?></dt>
<dd>
<?php comment_text() ?>
<?php
/* -- whisper add s -- */
} else if('0' == $comment->comment_approved){
?>
<dt id="comment<?php comment_ID() ?>"><span class="name">******</span> <span class="date"><?php comment_date(__('y-m-d (D) G:i', 'vicuna')) ?></span> </dt>
<dd>
<?php
_e('This comment is awaiting the approval of the author','vicuna');
}
/* -- whisper add e -- */
?>
</dd>
<?php endforeach; ?>
</dl>
<?php } ?>
<?php endif;
これ以外は以前紹介しているコメントを入れ子方式に対応させましたと同様にしています。
そして更に追加でピンバック・トラックバックに関しても承認の是非に関わるソースコードを追加してみました。それがこちら。ちなみにこちらに関しては動作しているかどうかは試していませんのであしからず……。この記事をピンバックしてみて動作していることを確認しました。
if ($trackpingCount > 0) :
vicuna_edit_comments_link(__('Edit this comments.', 'vicuna'), '<p class="admin">', '</p>'); ?>
<dl class="log">
<?php foreach ($trackpingArray as $comment) : ?>
<?php
if('1' == $comment->comment_approved){
?>
<dt id="ping<?php comment_ID() ?>"><span class="name"><?php printf(__("%s from %s", 'vicuna'), get_comment_type(), get_comment_author_link()); ?></span> <span class="date"><?php comment_date(__('y-m-d (D) G:i', 'vicuna')); ?></span></dt>
<dd>
<?php comment_text() ?>
<?php
} else if('0' == $comment->comment_approved){
?>
<dt id="ping<?php comment_ID() ?>"><span class="name">******</span> <span class="date"><?php comment_date(__('y-m-d (D) G:i', 'vicuna')); ?></span></dt>
<dd>
<?php
_e('This comment is awaiting the approval of the author','vicuna');
}
?>
</dd>
<?php endforeach; ?>
</dl>
<?php endif; ?>
ピンバック・トラックバックに関してはこれで大丈夫かわかりませんが、もし良かったら試してみてください(^-^)
[追記]
この記事の一番最初にご紹介したfunction.phpの末端に付加するコードですが、スパムコメントに関して少々問題があったので初回公開時のコードから修正してます。またトラックバック・ピンバックの表示についても同様に問題があったので修正しました。(といってもif('0' == $comment->comment_approved)を付加しただけですけど)
またコメントとピンバック/トラックバックの数の中にスパムのものもカウントされてしまっていましたのでcomments.phpとfunction.phpのソースコードも下記のように一部変更しました。
まずは1つの記事を表示した際に表示されるコメントとピンバック/トラックバックの数についてはcomments.phpを下記のように改変します。
/* to split comment and pings */
$trackpingCount = 0;
$commentCount = 0;
/* -- whisper add s -- */
global $wpdb;
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_date", $post->ID));
/* -- whisper add e -- */
if ($comments) :
foreach ($comments as $comment) {
$type = get_comment_type();
$approved = $comment->comment_approved;
switch( $type ) {
case 'trackback' :
case 'pingback' :
if ( $approved != 'spam') : $trackpingArray[$trackpingCount++] = $comment; endif;
break;
default :
if ( $approved != 'spam') : $commentArray[$commentCount++] = $comment; endif;
}
}
endif;
そして記事一覧の記事ごとのコメントとピンバック/トラックバックの数についてはfunction.phpの中を下記のように変更しました。
* Return the total amount of pings.
*/
function get_vicuna_pings_count() {
global $post, $wpdb, $id;
/* -- whisper cng s --
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' AND comment_type != '' ORDER BY comment_date");
-- */
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' AND (comment_type = 'trackback' OR comment_type = 'pingback') ORDER BY comment_date");
/* -- whisper cng e -- */
// $comments = apply_filters( 'comments_array', $comments, $post->ID );
return count($comments);
}
/* -- whisper add s -- */
function get_vicuna_comment_count() {
global $post, $wpdb, $id, $comments;
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND comment_approved = '1' order by comment_date");
return count($comments);
}
/* -- whisper add e -- */
これでスパムコメント・ピンバックはカウントされなくなると思います。
- 関連記事:
関連記事は今のところありません
Comments:0
Trackbacks:1
- Trackback URL for this entry
- http://shinonon-web.net/2009/05/04/728/trackback/
- Listed below are links to weblogs that reference
- コメントの入れ子方式について(vicuna CMS Ext. Customに対して) from ある日の小さなつぶやき
- pingback from wp.vicuna.CMS.Exp.Customでスパムコメント・トラックバックが公開される件(対策法) - ある日の小さなつぶやき 09-05-04 (月) 19:16
-
[...] Older [...]











