Repository navigation
Comments: Introduce a register_comment_type() API #12311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adamsilverstein
wants to merge
43
commits into
WordPress:trunk
Choose a base branch
from
adamsilverstein:feature/register-comment-type
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 ebb51c5
Comments: Add tests for the comment type registration API.
adamsilverstein 506e9c6
Comments: Fix default labels for comment types registered without a l…
adamsilverstein 81eceff
Comments: Escape the comment type label in comment_type().
adamsilverstein 3e1551b
Tests: Reset comment types between tests in the shared test framework.
adamsilverstein 2c7c495
Comments: Remove the unused 'show_ui' argument from comment type regi…
adamsilverstein bb81be9
Comments: Reject re-registration of built-in comment types.
adamsilverstein 9efe27b
Docs: Correct and clarify comment type registration documentation.
adamsilverstein 6a1ea10
Comments: Keep post-type-only labels out of comment type labels objects.
adamsilverstein bacdf27
Merge remote-tracking branch 'origin/trunk' into feature/register-com…
adamsilverstein 413d677
Build/Test Tools: Reset the whole comment type registry between tests.
adamsilverstein 11a4975
Comments: Ignore a hierarchical argument when registering a comment t…
adamsilverstein e9e894c
Comments: Reserve the query token names and spell out the argument co…
adamsilverstein 1504bdf
Comments: Stop get_comment_type_labels() from modifying the passed ob…
adamsilverstein 8a21e22
Docs: Describe the internal comment type flag as intent, not behavior.
adamsilverstein 16b2c4f
Build/Test Tools: Drop the unused _unregister_comment_type() helper.
adamsilverstein 1473c5b
Comments: Stamp the comment type API for 7.2.0.
adamsilverstein 4cf1917
Merge branch 'trunk' into feature/register-comment-type
adamsilverstein 5a7994b
Comments: Tighten types in the comment type API.
adamsilverstein 1b6c207
Tests: Add types to the comment type API tests.
adamsilverstein 486802b
Merge remote-tracking branch 'origin/trunk' into feature/register-com…
adamsilverstein c4c88a3
Comments: Register the `reaction` comment type as a built-in.
adamsilverstein 249caab
Comments: Tighten comment type inline documentation.
adamsilverstein ea9622a
Docs: Mark the new comment type APIs as @since 7.3.0.
adamsilverstein bfe3057
Merge remote-tracking branch 'origin/trunk' into feature/register-com…
adamsilverstein 6f7932a
Comments: Show the registered label for note and reaction in comment_…
adamsilverstein 9b545fb
Comments: Harden comment type registration.
adamsilverstein 7003333
Comments: Drop native parameter types from the comment type API.
adamsilverstein 743bb66
Comments: Reject '0' as a comment type name.
adamsilverstein 7bac4e2
Comments: Exclude internal comment types from queries by default.
adamsilverstein 025625a
Merge branch 'trunk' into feature/register-comment-type
westonruter 4ecc11f
Add WP_Comment_Type to paths with strict PHPStan checks
westonruter acdd693
Comments: Add native types to WP_Comment_Type.
adamsilverstein 245d585
Apply batched suggestions from code review
adamsilverstein 65003f8
Comments: Add a conditional PHPStan return type to get_comment_types().
adamsilverstein ecb5ffd
Tests: Remove the non-array arguments test for register_comment_type().
adamsilverstein d40c54f
Merge branch 'trunk' into feature/register-comment-type
adamsilverstein 71ed7e4
Comments: Type the get_comment_type_object() parameter as a string.
adamsilverstein 85fff2e
Comments: Make the comment_type_exists() check explicit.
adamsilverstein 2d7ee3c
Comments: Guard the excluded comment types with an explicit array check.
adamsilverstein ed9ebe3
Docs: Correct the comment type hook priority comments.
adamsilverstein 1e7fa22
Comments: Add a PHPStan type for the comment type arguments.
adamsilverstein b1705ed
Docs: Drop the accepted arguments note from create_initial_comment_ty…
adamsilverstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if a |
||
| * | ||
| * @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(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.