Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/wp-includes/class-walker-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public function display_element( $element, &$children_elements, $max_depth, $dep
* @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.
* @since 7.1.0 Comments of a registered comment type with a `render_callback`
* are rendered via that callback.
*
* @see Walker::start_el()
* @see wp_list_comments()
Expand Down Expand Up @@ -185,6 +187,19 @@ public function start_el( &$output, $data_object, $depth = 0, $args = array(), $
return;
}

/*
* Allow a registered comment type to render itself. An explicit `callback`
* argument passed to wp_list_comments() takes precedence and is handled above.
*/
$comment_type_object = get_comment_type_object( $comment->comment_type );

if ( $comment_type_object && is_callable( $comment_type_object->render_callback ) ) {
ob_start();
call_user_func( $comment_type_object->render_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 );
}
Expand Down
53 changes: 48 additions & 5 deletions src/wp-includes/class-wp-comment-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,48 @@ final class WP_Comment_Type {
*/
public $_builtin = false;

/**
* Callback used to render a comment of this type in comment lists.
*
* When set to a callable, {@see Walker_Comment} invokes it to render a
* comment of this type, receiving the same arguments as the `callback`
* argument of wp_list_comments(): the comment object, the arguments array,
* and the depth. The precedence chain is: an explicit `callback` passed to
* wp_list_comments(), then this callback, then the default markup.
*
* Like the `callback` argument of wp_list_comments(), the callback must only
* output the opening of the list element; {@see Walker_Comment::end_el()}
* (or the `end-callback` argument) closes the element after any child
* comments have been rendered. Which element that is depends on the `style`
* argument, so the callback has to open a `<div>` when `$args['style']` is
* 'div' and an `<li>` otherwise, the way {@see Walker_Comment::comment()}
* does. Opening the wrong one leaves the markup mismatched.
*
* The callback must echo its output. Unlike the `render_callback` argument
* of register_block_type(), a returned string is discarded.
*
* Output from the callback is printed unescaped; the callback is
* responsible for escaping all output.
*
* The built-in comment types register without a callback and cannot be
* re-registered, but setting one on them through the
* {@see 'register_comment_type_args'} filter is supported. It grants no more
* than the `callback` argument of wp_list_comments() already does. Note that
* a callback on the 'comment' type also takes over the walker's handling of
* unapproved comments, which strips links from a pending comment's text for
* everyone but its author.
*
* Only applies when comments are rendered via wp_list_comments() (classic
* themes). Block themes render comments through the `core/comment-template`
* block and do not invoke this callback.
*
* Default null.
*
* @since 7.1.0
* @var callable|null
*/
public $render_callback = null;

/**
* Whether the comment type is hierarchical.
*
Expand Down Expand Up @@ -183,11 +225,12 @@ public function set_props( $args ) {
* treated as a provided value and overwrite the default name with false.
*/
$defaults = array(
'labels' => array(),
'description' => '',
'public' => true,
'internal' => false,
'_builtin' => false,
'labels' => array(),
'description' => '',
'public' => true,
'internal' => false,
'render_callback' => null,
'_builtin' => false,
);

$args = array_merge( $defaults, $args );
Expand Down
45 changes: 29 additions & 16 deletions src/wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,22 +434,35 @@ function create_initial_comment_types() {
* @param array|string $args {
* Optional. Array or string of arguments for registering a comment type. Default empty array.
*
* @type string $label Name of the comment type. Usually plural.
* Default is the value of $labels['name'].
* @type string[] $labels An array of labels for this comment type. If not set, the
* default comment labels are used. See get_comment_type_labels()
* for a full list of supported labels.
* @type string $description A short descriptive summary of what the comment type is.
* Default empty.
* @type bool $public Whether the comment type is intended for use publicly either via
* the admin interface or by front-end users. Core does not
* currently act on this argument. Default true.
* @type bool $internal Whether the comment type is for internal use only. Internal types
* are excluded from comment queries and counts by default, through
* the {@see 'default_excluded_comment_types'} filter. Default false.
* @type bool $_builtin For internal core use only. Marks the type as native to
* WordPress, which blocks it from being re-registered or
* unregistered. Default false.
* @type string $label Name of the comment type. Usually plural.
* Default is the value of $labels['name'].
* @type string[] $labels An array of labels for this comment type. If not set, the
* default comment labels are used. See get_comment_type_labels()
* for a full list of supported labels.
* @type string $description A short descriptive summary of what the comment type is.
* Default empty.
* @type bool $public Whether the comment type is intended for use publicly either
* via the admin interface or by front-end users. Core does not
* currently act on this argument. Default true.
* @type bool $internal Whether the comment type is for internal use only. Internal
* types are excluded from comment queries and counts by
* default, through the
* {@see 'default_excluded_comment_types'} filter.
* Default false.
* @type callable $render_callback Callback used to render a comment of this type in comment
* lists. Receives the same arguments as the `callback` argument
* of wp_list_comments() (the comment, the arguments, and the
* depth) and, like it, must echo only the opening of the list
* element - a `<div>` when the `style` argument is 'div' and an
* `<li>` otherwise; Walker_Comment::end_el() closes the element.
* A returned string is discarded. An explicit
* wp_list_comments() `callback` takes precedence. Output is
* printed unescaped; the callback must escape all output.
* Applies only when comments are rendered via
* wp_list_comments() (classic themes). Default null.
* @type bool $_builtin For internal core use only. Marks the type as native to
* WordPress, which blocks it from being re-registered or
* unregistered. Default false.
* }
* @return WP_Comment_Type|WP_Error The registered comment type object on success,
* WP_Error object on failure.
Expand Down
Loading
Loading