Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
0904ce2
Comments: Introduce a register_comment_type() API.
adamsilverstein Jun 24, 2026
ebb51c5
Comments: Add tests for the comment type registration API.
adamsilverstein Jun 24, 2026
506e9c6
Comments: Fix default labels for comment types registered without a l…
adamsilverstein Jun 24, 2026
81eceff
Comments: Escape the comment type label in comment_type().
adamsilverstein Jun 25, 2026
3e1551b
Tests: Reset comment types between tests in the shared test framework.
adamsilverstein Jul 11, 2026
2c7c495
Comments: Remove the unused 'show_ui' argument from comment type regi…
adamsilverstein Jul 11, 2026
bb81be9
Comments: Reject re-registration of built-in comment types.
adamsilverstein Jul 11, 2026
9efe27b
Docs: Correct and clarify comment type registration documentation.
adamsilverstein Jul 11, 2026
6a1ea10
Comments: Keep post-type-only labels out of comment type labels objects.
adamsilverstein Jul 11, 2026
bacdf27
Merge remote-tracking branch 'origin/trunk' into feature/register-com…
adamsilverstein Aug 12, 2026
413d677
Build/Test Tools: Reset the whole comment type registry between tests.
adamsilverstein Aug 12, 2026
11a4975
Comments: Ignore a hierarchical argument when registering a comment t…
adamsilverstein Aug 12, 2026
e9e894c
Comments: Reserve the query token names and spell out the argument co…
adamsilverstein Aug 12, 2026
1504bdf
Comments: Stop get_comment_type_labels() from modifying the passed ob…
adamsilverstein Aug 23, 2026
8a21e22
Docs: Describe the internal comment type flag as intent, not behavior.
adamsilverstein Aug 23, 2026
16b2c4f
Build/Test Tools: Drop the unused _unregister_comment_type() helper.
adamsilverstein Aug 23, 2026
1473c5b
Comments: Stamp the comment type API for 7.2.0.
adamsilverstein Aug 23, 2026
4cf1917
Merge branch 'trunk' into feature/register-comment-type
adamsilverstein Nov 8, 2026
5a7994b
Comments: Tighten types in the comment type API.
adamsilverstein Nov 8, 2026
1b6c207
Tests: Add types to the comment type API tests.
adamsilverstein Nov 8, 2026
486802b
Merge remote-tracking branch 'origin/trunk' into feature/register-com…
adamsilverstein Nov 8, 2026
c4c88a3
Comments: Register the `reaction` comment type as a built-in.
adamsilverstein Nov 8, 2026
249caab
Comments: Tighten comment type inline documentation.
adamsilverstein Nov 8, 2026
ea9622a
Docs: Mark the new comment type APIs as @since 7.3.0.
adamsilverstein Nov 8, 2026
bfe3057
Merge remote-tracking branch 'origin/trunk' into feature/register-com…
adamsilverstein Nov 18, 2026
6f7932a
Comments: Show the registered label for note and reaction in comment_…
adamsilverstein Nov 18, 2026
9b545fb
Comments: Harden comment type registration.
adamsilverstein Nov 18, 2026
7003333
Comments: Drop native parameter types from the comment type API.
adamsilverstein Nov 18, 2026
743bb66
Comments: Reject '0' as a comment type name.
adamsilverstein Nov 18, 2026
7bac4e2
Comments: Exclude internal comment types from queries by default.
adamsilverstein Nov 19, 2026
025625a
Merge branch 'trunk' into feature/register-comment-type
westonruter Nov 19, 2026
4ecc11f
Add WP_Comment_Type to paths with strict PHPStan checks
westonruter Nov 19, 2026
acdd693
Comments: Add native types to WP_Comment_Type.
adamsilverstein Nov 19, 2026
245d585
Apply batched suggestions from code review
adamsilverstein Nov 19, 2026
65003f8
Comments: Add a conditional PHPStan return type to get_comment_types().
adamsilverstein Nov 19, 2026
ecb5ffd
Tests: Remove the non-array arguments test for register_comment_type().
adamsilverstein Nov 19, 2026
d40c54f
Merge branch 'trunk' into feature/register-comment-type
adamsilverstein Nov 21, 2026
71ed7e4
Comments: Type the get_comment_type_object() parameter as a string.
adamsilverstein Nov 29, 2026
85fff2e
Comments: Make the comment_type_exists() check explicit.
adamsilverstein Nov 29, 2026
2d7ee3c
Comments: Guard the excluded comment types with an explicit array check.
adamsilverstein Nov 29, 2026
ed9ebe3
Docs: Correct the comment type hook priority comments.
adamsilverstein Nov 29, 2026
1e7fa22
Comments: Add a PHPStan type for the comment type arguments.
adamsilverstein Nov 29, 2026
b1705ed
Docs: Drop the accepted arguments note from create_initial_comment_ty…
adamsilverstein Nov 29, 2026
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
1 change: 1 addition & 0 deletions phpstan-strict.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ parameters:
- src/wp-includes/class-wp-block-styles-registry.php
- src/wp-includes/class-wp-block-template.php
- src/wp-includes/class-wp-comment.php
- src/wp-includes/class-wp-comment-type.php
- src/wp-includes/class-wp-connector-registry.php
- src/wp-includes/class-wp-error.php
- src/wp-includes/class-wp-post.php
Expand Down
251 changes: 251 additions & 0 deletions src/wp-includes/class-wp-comment-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
<?php
/**
* Comment API: WP_Comment_Type class
*
* @package WordPress
* @subpackage Comments
* @since 7.3.0
*/

/**
* Core class used for interacting with comment types.
*
* @since 7.3.0
*
* @see register_comment_type()
*
* @phpstan-type Comment_Type_Args array{
* label?: string,
* labels?: string[],
* description?: string,
* public?: bool,
* internal?: bool,
* _builtin?: bool,
* ...
* }
*/
#[AllowDynamicProperties]
final class WP_Comment_Type {
Comment thread
adamsilverstein marked this conversation as resolved.
Resolved
/**
* Comment type key.
*
* @since 7.3.0
*/
public string $name;

/**
* Name of the comment type. Usually plural.
*
* @since 7.3.0
*/
public string $label;

/**
* Labels object for this comment type.
*
* If not set, the default comment labels are used.
*
* @see get_comment_type_labels()
*
* @since 7.3.0
*/
public stdClass $labels;

/**
* Default labels.
*
* @since 7.3.0
* @var array<string, array<string|null>> $default_labels
* @phpstan-var array{
* name: array{ string, null },
* singular_name: array{ string, null },
* }|array{}
*/
protected static array $default_labels = array();

/**
* A short descriptive summary of what the comment type is for.
*
* @since 7.3.0
*/
public string $description = '';

/**
* Whether a comment type is intended for use publicly either via the admin interface or by front-end users.
*
* Core does not currently act on this property.
*
* Default true.
*
* @since 7.3.0
*/
public bool $public = true;

/**
* Whether the comment type is for internal use only.
*
* Internal types are meant to be excluded from comment queries and counts by
* default. Core does not currently act on this property.
*
* Default false.
*
* @since 7.3.0
*/
public bool $internal = false;

/**
* Whether this comment type is a native or "built-in" comment type.
*
* Default false.
*
* @since 7.3.0
*/
public bool $_builtin = false;

/**
* Constructor.
*
* See the register_comment_type() function for accepted arguments for `$args`.
*
* Will populate object properties from the provided arguments and assign other
* default properties based on that information.
*
* @since 7.3.0
*
* @see register_comment_type()
*
* @param string $comment_type Comment type key.
* @param array<string, mixed> $args Optional. Array of arguments for registering
* a comment type. Default empty array.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if a @phpstan-type were added to the class that had the full array shape defined in the phpdoc hash on register_comment_type(). This could then be re-used here in a @phpstan-param and then also in the set_props() method, and the docblocks for the filters.

*
* @phpstan-param Comment_Type_Args $args
*/
public function __construct( string $comment_type, array $args = array() ) {
$this->name = $comment_type;

$this->set_props( $args );
}

/**
* Sets comment type properties.
*
* See the register_comment_type() function for accepted arguments for `$args`.
*
* @since 7.3.0
*
* @param array<string, mixed> $args Array of arguments for registering a comment type.
*
* @phpstan-param Comment_Type_Args $args
*/
public function set_props( array $args ): void {
// Captured before the filters run, so they cannot add or remove built-in status.
$is_builtin = ! empty( $args['_builtin'] );

/**
* Filters the arguments for registering a comment type.
*
* Changes to the `_builtin` argument are ignored.
*
* @since 7.3.0
*
* @param array $args Array of arguments for registering a comment type.
* See the register_comment_type() function for accepted arguments.
* @param string $comment_type Comment type key.
*
* @phpstan-param Comment_Type_Args $args
*/
$args = apply_filters( 'register_comment_type_args', $args, $this->name );

$comment_type = $this->name;

/**
* Filters the arguments for registering a specific comment type.
*
* The dynamic portion of the filter name, `$comment_type`, refers to the comment type key.
* Changes to the `_builtin` argument are ignored.
*
* Possible hook names include:
*
* - `register_comment_comment_type_args`
* - `register_pingback_comment_type_args`
*
* @since 7.3.0
*
* @param array $args Array of arguments for registering a comment type.
* See the register_comment_type() function for accepted arguments.
* @param string $comment_type Comment type key.
*
* @phpstan-param Comment_Type_Args $args
*/
$args = apply_filters( "register_{$comment_type}_comment_type_args", $args, $this->name );

// No 'label' default, so get_comment_type_labels() can fall back to the default name.
$defaults = array(
'labels' => array(),
'description' => '',
'public' => true,
'internal' => false,
'_builtin' => false,
);

$args = array_merge( $defaults, (array) $args );

$args['name'] = $this->name;
$args['_builtin'] = $is_builtin;

// The filters can return anything, so match the declared property types before assigning.
$args['labels'] = is_array( $args['labels'] ) || is_object( $args['labels'] ) ? (object) (array) $args['labels'] : new stdClass();
$args['description'] = is_string( $args['description'] ) ? $args['description'] : '';
$args['public'] = (bool) $args['public'];
$args['internal'] = (bool) $args['internal'];

if ( isset( $args['label'] ) && ! is_string( $args['label'] ) ) {
unset( $args['label'] );
}

foreach ( $args as $property_name => $property_value ) {
$this->$property_name = $property_value;
}

$this->labels = get_comment_type_labels( $this );
$this->label = $this->labels->name;
}

/**
* Returns the default labels for comment types.
*
* @since 7.3.0
*
* Each label is a pair of non-hierarchical and hierarchical values, matching the
* post type and taxonomy shape. Comment types are never hierarchical, so only the
* first value is used.
*
* @return array<string, array<string|null>> The default labels for comment types.
*
* @phpstan-return array{
* name: array{ string, null },
* singular_name: array{ string, null },
* }
*/
public static function get_default_labels(): array {
if ( ! empty( self::$default_labels ) ) {
return self::$default_labels;
}

self::$default_labels = array(
'name' => array( _x( 'Comments', 'comment type general name' ), null ),
'singular_name' => array( _x( 'Comment', 'comment type singular name' ), null ),
);

return self::$default_labels;
}

/**
* Resets the cache for the default labels.
*
* @since 7.3.0
*/
public static function reset_default_labels(): void {
self::$default_labels = array();
}
}
13 changes: 12 additions & 1 deletion src/wp-includes/comment-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1195,12 +1195,16 @@ function get_comment_type( $comment_id = 0 ) {
* Displays the comment type of the current comment.
*
* @since 0.71
* @since 7.3.0 The default output for a registered comment type other than 'comment',
* such as 'note', falls back to the type's singular name label.
*
* @param string|false $comment_text Optional. String to display for comment type. Default false.
* @param string|false $trackback_text Optional. String to display for trackback type. Default false.
* @param string|false $pingback_text Optional. String to display for pingback type. Default false.
*/
function comment_type( $comment_text = false, $trackback_text = false, $pingback_text = false ) {
$comment_text_overridden = ( false !== $comment_text );

if ( false === $comment_text ) {
$comment_text = _x( 'Comment', 'noun' );
}
Expand All @@ -1219,7 +1223,14 @@ function comment_type( $comment_text = false, $trackback_text = false, $pingback
echo $pingback_text;
break;
default:
echo $comment_text;
// Other registered types fall back to their singular label.
$comment_type_object = ( $comment_text_overridden || 'comment' === $type ) ? null : get_comment_type_object( $type );

if ( $comment_type_object && is_string( $comment_type_object->labels->singular_name ?? null ) ) {
echo esc_html( $comment_type_object->labels->singular_name );
} else {
echo $comment_text;
}
}
}

Expand Down
Loading
Loading