类文档

Walker_Comment

💡 云策文档标注

概述

Walker_Comment 是 WordPress 核心的 Walker 类,专门用于生成评论的 HTML 列表结构。它继承自 Walker 类,通过一系列方法控制评论的遍历和输出,支持嵌套评论和多种格式。

关键要点

  • 继承自 Walker 类,用于处理评论树结构,生成 HTML 列表。
  • 定义了 $tree_type 为 'comment' 和 $db_fields 数组,指定数据库字段映射。
  • 包含多个核心方法,如 start_lvl、end_lvl、start_el、end_el 等,用于控制列表和元素的开始与结束。
  • display_element 方法增强遍历逻辑,防止深层嵌套评论被孤立。
  • 支持不同评论类型(如 comment、pingback、trackback)和输出格式(如 HTML5)。
  • 可通过回调函数自定义输出,并包含 filter_comment_text 方法过滤评论文本。

代码示例

public function start_lvl( &$output, $depth = 0, $args = array() ) {
    $GLOBALS['comment_depth'] = $depth + 1;

    switch ( $args['style'] ) {
        case 'div':
            break;
        case 'ol':
            $output .= '' . "n";
            break;
        case 'ul':
        default:
            $output .= '' . "n";
            break;
    }
}

注意事项

使用 Walker_Comment 时,需注意参数传递和全局变量(如 $comment_depth)的管理,以确保正确输出评论层级。在自定义输出时,可覆盖方法或使用回调函数,但需遵循 Walker 类的结构。


📄 原文内容

Core walker class used to create an HTML list of comments.

Description

See also

Methods

Name Description
Walker_Comment::comment Outputs a single comment.
Walker_Comment::display_element Traverses elements to create list from elements.
Walker_Comment::end_el Ends the element output, if needed.
Walker_Comment::end_lvl Ends the list of items after the elements are added.
Walker_Comment::filter_comment_text Filters the comment text.
Walker_Comment::html5_comment Outputs a comment in the HTML5 format.
Walker_Comment::ping Outputs a pingback comment.
Walker_Comment::start_el Starts the element output.
Walker_Comment::start_lvl Starts the list before the elements are added.

Source

class Walker_Comment extends Walker {

/**
* What the class handles.
*
* @since 2.7.0
* @var string
*
* @see Walker::$tree_type
*/
public $tree_type = 'comment';

/**
* Database fields to use.
*
* @since 2.7.0
* @var string[]
*
* @see Walker::$db_fields
* @todo Decouple this
*/
public $db_fields = array(
'parent' => 'comment_parent',
'id' => 'comment_ID',
);

/**
* Starts the list before the elements are added.
*
* @since 2.7.0
*
* @see Walker::start_lvl()
* @global int $comment_depth
*
* @param string $output Used to append additional content (passed by reference).
* @param int $depth Optional. Depth of the current comment. Default 0.
* @param array $args Optional. Uses 'style' argument for type of HTML list. Default empty array.
*/
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$GLOBALS['comment_depth'] = $depth + 1;

switch ( $args['style'] ) {
case 'div':
break;
case 'ol':
$output .= '

    ' . "n";
    break;
    case 'ul':
    default:
    $output .= '

      ' . "n";
      break;
      }
      }

      /**
      * Ends the list of items after the elements are added.
      *
      * @since 2.7.0
      *
      * @see Walker::end_lvl()
      * @global int $comment_depth
      *
      * @param string $output Used to append additional content (passed by reference).
      * @param int $depth Optional. Depth of the current comment. Default 0.
      * @param array $args Optional. Will only append content if style argument value is 'ol' or 'ul'.
      * Default empty array.
      */
      public function end_lvl( &$output, $depth = 0, $args = array() ) {
      $GLOBALS['comment_depth'] = $depth + 1;

      switch ( $args['style'] ) {
      case 'div':
      break;
      case 'ol':
      $output .= "

n";
break;
case 'ul':
default:
$output .= "

n";
break;
}
}

/**
* Traverses elements to create list from elements.
*
* This function is designed to enhance Walker::display_element() to
* display children of higher nesting levels than selected inline on
* the highest depth level displayed. This prevents them being orphaned
* at the end of the comment list.
*
* Example: max_depth = 2, with 5 levels of nested content.
* 1
* 1.1
* 1.1.1
* 1.1.1.1
* 1.1.1.1.1
* 1.1.2
* 1.1.2.1
* 2
* 2.2
*
* @since 2.7.0
*
* @see Walker::display_element()
* @see wp_list_comments()
*
* @param WP_Comment $element Comment data object.
* @param array $children_elements List of elements to continue traversing. Passed by reference.
* @param int $max_depth Max depth to traverse.
* @param int $depth Depth of the current element.
* @param array $args An array of arguments.
* @param string $output Used to append additional content. Passed by reference.
*/
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
if ( ! $element ) {
return;
}

$id_field = $this->db_fields['id'];
$id = $element->$id_field;

parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );

/*
* If at the max depth, and the current element still has children, loop over those
* and display them at this level. This is to prevent them being orphaned to the end
* of the list.
*/
if ( $max_depth display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
}

unset( $children_elements[ $id ] );
}
}

/**
* Starts the element output.
*
* @since 2.7.0
* @since 5.9.0 Renamed `$comment` to `$data_object` and `$id` to `$current_object_id`
* to match parent class for PHP 8 named parameter support.
*
* @see Walker::start_el()
* @see wp_list_comments()
* @global int $comment_depth
* @global WP_Comment $comment Global comment object.
*
* @param string $output Used to append additional content. Passed by reference.
* @param WP_Comment $data_object Comment data object.
* @param int $depth Optional. Depth of the current comment in reference to parents. Default 0.
* @param array $args Optional. An array of arguments. Default empty array.
* @param int $current_object_id Optional. ID of the current comment. Default 0.
*/
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
// Restores the more descriptive, specific name for use within this method.
$comment = $data_object;

++$depth;
$GLOBALS['comment_depth'] = $depth;
$GLOBALS['comment'] = $comment;

if ( ! empty( $args['callback'] ) ) {
ob_start();
call_user_func( $args['callback'], $comment, $args, $depth );
$output .= ob_get_clean();
return;
}

if ( 'comment' === $comment->comment_type ) {
add_filter( 'comment_text', array( $this, 'filter_comment_text' ), 40, 2 );
}

if ( ( 'pingback' === $comment->comment_type || 'trackback' === $comment->comment_type ) && $args['short_ping'] ) {
ob_start();
$this->ping( $comment, $depth, $args );
$output .= ob_get_clean();
} elseif ( 'html5' === $args['format'] ) {
ob_start();
$this->html5_comment( $comment, $depth, $args );
$output .= ob_get_clean();
} else {
ob_start();
$this->comment( $comment, $depth, $args );
$output .= ob_get_clean();
}

if ( 'comment' === $comment->comment_type ) {
remove_filter( 'comment_text', array( $this, 'filter_comment_text' ), 40 );
}
}

/**
* Ends the element output, if needed.
*
* @since 2.7.0
* @since 5.9.0 Renamed `$comment` to `$data_object` to match parent class for PHP 8 named parameter support.
*
* @see Walker::end_el()
* @see wp_list_comments()
*
* @param string $output Used to append additional content. Passed by reference.
* @param WP_Comment $data_object Comment data object.
* @param int $depth Optional. Depth of the current comment. Default 0.
* @param array $args Optional. An array of arguments. Default empty array.
*/
public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
if ( ! empty( $args['end-callback'] ) ) {
ob_start();
call_user_func(
$args['end-callback'],
$data_object, // The current comment object.
$args,
$depth
);
$output .= ob_get_clean();
return;
}
if ( 'div' === $args['style'] ) {
$output .= "