我有一个回调函数用于循环浏览我博客中文章评论的另一个函数。我现在尝试切换到“线程/嵌套”注释,从而扩展了回调函数。到目前为止一切正常,但我无法摆脱这种感觉,我没有按照最好的PHP练习(和性能)来编写它。
我使用css框架,并且必须为我分配给单个注释的.span-xy
类做一些数学运算。我从一个全局常量和输出的输入值开始,例如。 span-12
表示父评论。然后我必须为每个嵌套级别降低/升高每+/- (int) 1
的值。所以我来构建数组,计算它们,迭代并为每个注释构建临时数组。
问题: 有没有更简单的方法解决这个问题?
<!-- This is the final html mark-up output: List of comments (threaded/nested) -->
<ul>
<li id="1" class="push-<?php echo $push; ?> span-<?php echo $span; ?>">comment - parent</li>
<li id="2">comment - child of #1
<ul class="children">
<li id="3">comment - child of #2
<li id="4">comment - child of #2
<ul class="children">
<li id="5">comment - child of #4</li>
</ul>
<li id="6">comment - child of #2</li>
</ul>
<li id="7">comment - child of #2
<ul class="children">
<li id="8">comment - child of #7</li>
<li id="9">comment - child of #7</li>
</ul>
</li>
</li>
</ul>
<?php
// This is my callback function
function comment_list_cb( $comment, $args, $depth )
{
// retrieve the data from the globals or make them available
$GLOBALS['comment'] = $comment;
global $post;
static $width = MY_GLOBAL_WIDTH_CONSTANT;
static $ancestors = null;
// is Child/Parent comment
$parent = (int) $comment->comment_parent; // retrieve the ID of the parent
$is_child = false;
if ( $parent > (int) 0 ) // if we got a parent
{
$is_child = true;
if ( ! (array) $ancestors )
$ancestors = array();
if ( ! array_key_exists( $parent, $ancestors ) )
{
$ancestors[$parent] = get_comment_ID();
}
else
{
foreach ( $ancestors as $parent_id => $child_id )
{
if ( $parent_id == $parent )
{
$ancestors_temp[$parent_id] = $child_id;
break;
}
$ancestors_temp[$parent_id] = $child_id;
}
$ancestors = $ancestors_temp;
}
$parent_counter = count( $ancestors );
$span = $width - (int) $parent_counter;
}
else
{
$ancestors = $parent_counter = null;
$span = MY_GLOBAL_WIDTH_CONSTANT;
}
$span_txt = $span - (int) 2; // reduce per `2` because of the span-2 class at the avatar element
// now: build the classes
$push = $parent_counter != (int) 0 ? 'push-1' : '';
$child = $is_child === true ? ' child ' : '';
$list = comment_class( 'span-'.$span.' '.$push.' append-bottom last hreview comment-'.get_comment_ID().' '.$microid, get_comment_ID(), $post->ID, false );
?>
<!-- build the comment -->
<li <?php echo $list; ?>>
<div id="<?php get_comment_ID(); ?>">
<span class="comment-avatar span-2"><!-- display avatar img - width is span-2 --></span>
<span class="comment-meta span-<?php echo $span_txt; ?> last"><!-- display meta data like timestamp, etc. --></span>
<span class="comment-text span-<?php echo $span_txt; ?> last"><!-- display message --></span>
</div>
</li>
<?php
}
答案 0 :(得分:1)
快速扫描后的一些一般性评论。它涉及编码,无论功能如何。
$GLOBALS['comment'] = $comment;
为什么要把它放在全球范围内?这可以覆盖现有的全局变量。通过引用传递可能更合适。
static $width = MY_GLOBAL_WIDTH_CONSTANT;
为什么这是静态的?价值永远不会改变,因此无需保留。
if ( $parent > (int) 0 )
[...]
$span_txt = $span - (int) 2;
[...]
$push = $parent_counter != (int) 0 ? 'push-1' : '';
无需将int literal转换为int。如果你想要int比较,那就是你应该投射的变量。
if ( ! (array) $ancestors )
$ancestors = array();
如果为空数组,请设置空数组?只需!isset($ancestors)