diff --git a/phpstan-strict.neon.dist b/phpstan-strict.neon.dist index ffa58a226d576..4864d47a2e134 100644 --- a/phpstan-strict.neon.dist +++ b/phpstan-strict.neon.dist @@ -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 diff --git a/src/wp-includes/class-wp-comment-type.php b/src/wp-includes/class-wp-comment-type.php new file mode 100644 index 0000000000000..330aacf054d5a --- /dev/null +++ b/src/wp-includes/class-wp-comment-type.php @@ -0,0 +1,251 @@ +> $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 $args Optional. Array of arguments for registering + * a comment type. Default empty array. + * + * @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 $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> 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(); + } +} diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 86d52e1850ec4..a5ebc5523fb1c 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -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' ); } @@ -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; + } } } diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 6a1641cbae4d0..e198ec4833e1f 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -330,6 +330,407 @@ function get_comments( $args = '' ) { return $query->query( $args ); } +/** + * Creates the initial comment types when 'init' action is fired. + * + * @since 7.3.0 + */ +function create_initial_comment_types(): void { + global $wp_comment_types; + + if ( ! is_array( $wp_comment_types ) ) { + $wp_comment_types = array(); + } + + WP_Comment_Type::reset_default_labels(); + + /* + * The 'comment', 'pingback', and 'trackback' labels reuse existing translation + * strings; 'note' and 'reaction' use new strings with explicit contexts. + */ + $builtin_comment_types = array( + 'comment' => array( + 'label' => __( 'Comments' ), + 'labels' => array( + 'singular_name' => _x( 'Comment', 'noun' ), + ), + ), + 'pingback' => array( + 'label' => __( 'Pingbacks' ), + 'labels' => array( + 'singular_name' => __( 'Pingback' ), + ), + ), + 'trackback' => array( + 'label' => __( 'Trackbacks' ), + 'labels' => array( + 'singular_name' => __( 'Trackback' ), + ), + ), + 'note' => array( + 'label' => _x( 'Notes', 'comment type general name' ), + 'labels' => array( + 'singular_name' => _x( 'Note', 'comment type singular name' ), + ), + 'public' => false, + 'internal' => true, + ), + 'reaction' => array( + 'label' => _x( 'Reactions', 'comment type general name' ), + 'labels' => array( + 'singular_name' => _x( 'Reaction', 'comment type singular name' ), + ), + 'public' => false, + 'internal' => true, + ), + ); + + foreach ( $builtin_comment_types as $comment_type => $args ) { + // Re-registration of a built-in type is rejected, so clear it first ('init', 'change_locale'). + unset( $wp_comment_types[ $comment_type ] ); + + $args['_builtin'] = true; + + register_comment_type( $comment_type, $args ); + } +} + +/** + * Registers a comment type. + * + * Note: Comment type registrations should not be hooked before the {@see 'init'} action. + * Registering a comment type earlier can result in its labels being generated before + * the current locale's translations are loaded. + * + * Comment types are stored verbatim in the `comment_type` column of the comments table. + * Registration provides labels and metadata for a type; it does not constrain which values + * may be stored. Comment types that are stored but never registered keep behaving exactly + * as they did before this API existed. + * + * Like post types, registrations are not scoped to a site on multisite. + * + * Cannot be used to re-register built-in comment types. The names WP_Comment_Query reads + * as query tokens ('all', 'comments', 'pings') cannot be registered either. + * + * The key is stored and matched verbatim, so a key that sanitize_key() would change + * (for example one with uppercase letters) is rejected rather than silently rewritten. + * The key '0' is rejected too, since wp_insert_comment() would store it as 'comment'. + * + * @since 7.3.0 + * + * @global WP_Comment_Type[] $wp_comment_types List of comment types. + * + * @param string $comment_type Comment type key. Must not exceed 20 characters and may only + * contain lowercase alphanumeric characters, dashes, and underscores. + * See sanitize_key(). + * @param array $args { + * Optional. Array 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 meant to be excluded from comment queries and counts + * by default. Core does not currently act on this argument. + * 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. Cannot be changed by the + * {@see 'register_comment_type_args'} filters. Default false. + * } + * @return WP_Comment_Type|WP_Error The registered comment type object on success, + * WP_Error object on failure. + * + * @phpstan-param non-falsy-string $comment_type + */ +function register_comment_type( string $comment_type, array $args = array() ) { + global $wp_comment_types; + + if ( ! is_array( $wp_comment_types ) ) { + $wp_comment_types = array(); + } + + // Stored comment_type values are matched verbatim, so reject keys sanitize_key() would alter. + if ( sanitize_key( $comment_type ) !== $comment_type ) { + _doing_it_wrong( + __FUNCTION__, + __( 'Comment type names may only contain lowercase alphanumeric characters, dashes, and underscores.' ), + '7.3.0' + ); + return new WP_Error( 'comment_type_name_invalid', __( 'Comment type names may only contain lowercase alphanumeric characters, dashes, and underscores.' ) ); + } + + if ( '' === $comment_type || strlen( $comment_type ) > 20 ) { + _doing_it_wrong( __FUNCTION__, __( 'Comment type names must be between 1 and 20 characters in length.' ), '7.3.0' ); + return new WP_Error( 'comment_type_length_invalid', __( 'Comment type names must be between 1 and 20 characters in length.' ) ); + } + + // wp_insert_comment() treats an empty() type as 'comment', so '0' could never be stored. + if ( '0' === $comment_type ) { + _doing_it_wrong( __FUNCTION__, __( 'The "0" comment type name cannot be stored and is not allowed.' ), '7.3.0' ); + return new WP_Error( 'comment_type_name_invalid', __( 'The "0" comment type name cannot be stored and is not allowed.' ) ); + } + + if ( isset( $wp_comment_types[ $comment_type ] ) && $wp_comment_types[ $comment_type ]->_builtin ) { + _doing_it_wrong( + __FUNCTION__, + sprintf( + /* translators: %s: Comment type key. */ + __( 'The "%s" comment type is a built-in type and cannot be re-registered.' ), + $comment_type + ), + '7.3.0' + ); + return new WP_Error( 'comment_type_builtin', __( 'Built-in comment types cannot be re-registered.' ) ); + } + + // WP_Comment_Query treats these names as query tokens, not comment_type values. + if ( in_array( $comment_type, array( 'all', 'comments', 'pings' ), true ) ) { + _doing_it_wrong( + __FUNCTION__, + sprintf( + /* translators: %s: Comment type key. */ + __( 'The "%s" comment type name is reserved for use by WP_Comment_Query.' ), + $comment_type + ), + '7.3.0' + ); + return new WP_Error( 'comment_type_reserved', __( 'This comment type name is reserved.' ) ); + } + + $comment_type_object = new WP_Comment_Type( $comment_type, $args ); + + $wp_comment_types[ $comment_type ] = $comment_type_object; + + /** + * Fires after a comment type is registered. + * + * @since 7.3.0 + * + * @param string $comment_type Comment type key. + * @param WP_Comment_Type $comment_type_object Comment type object. + */ + do_action( 'registered_comment_type', $comment_type, $comment_type_object ); + + /** + * Fires after a specific comment type is registered. + * + * The dynamic portion of the hook name, `$comment_type`, refers to the comment type key. + * + * Possible hook names include: + * + * - `registered_comment_type_comment` + * - `registered_comment_type_pingback` + * + * @since 7.3.0 + * + * @param string $comment_type Comment type key. + * @param WP_Comment_Type $comment_type_object Comment type object. + */ + do_action( "registered_comment_type_{$comment_type}", $comment_type, $comment_type_object ); + + return $comment_type_object; +} + +/** + * Unregisters a comment type. + * + * Cannot be used to unregister built-in comment types. + * + * @since 7.3.0 + * + * @global array $wp_comment_types List of comment types. + * + * @param string $comment_type Comment type key. + * @return true|WP_Error True on success, WP_Error on failure or if the comment type doesn't exist. + */ +function unregister_comment_type( string $comment_type ) { + global $wp_comment_types; + + $comment_type_object = get_comment_type_object( $comment_type ); + + if ( ! $comment_type_object ) { + return new WP_Error( 'invalid_comment_type', __( 'Invalid comment type.' ) ); + } + + // Do not allow unregistering built-in comment types. + if ( $comment_type_object->_builtin ) { + return new WP_Error( 'invalid_comment_type', __( 'Unregistering a built-in comment type is not allowed.' ) ); + } + + unset( $wp_comment_types[ $comment_type ] ); + + /** + * Fires after a comment type is unregistered. + * + * @since 7.3.0 + * + * @param string $comment_type Comment type key. + */ + do_action( 'unregistered_comment_type', $comment_type ); + + return true; +} + +/** + * Retrieves a comment type object by name. + * + * @since 7.3.0 + * + * @global array $wp_comment_types List of comment types. + * + * @param string $comment_type The name of a registered comment type. + * @return WP_Comment_Type|null WP_Comment_Type object if it exists, null otherwise. + */ +function get_comment_type_object( string $comment_type ): ?WP_Comment_Type { + global $wp_comment_types; + + return $wp_comment_types[ $comment_type ] ?? null; +} + +/** + * Retrieves a list of registered comment type names or objects. + * + * @since 7.3.0 + * + * @global array $wp_comment_types List of comment types. + * + * @param array $args Optional. An array of key => value arguments to match against + * the comment type objects. Default empty array. + * @param string $output Optional. The type of output to return. Either comment type 'names' + * or 'objects'. Default 'names'. + * @param string $operator Optional. The logical operation to perform. 'or' means only one + * element from the array needs to match; 'and' means all elements + * must match; 'not' means no elements may match. Default 'and'. + * @return string[]|WP_Comment_Type[] An array of comment type names or objects. + * + * @phpstan-param 'and'|'AND'|'or'|'OR'|'not'|'NOT' $operator + * @phpstan-return ( $output is 'names' ? array : array ) + */ +function get_comment_types( $args = array(), $output = 'names', $operator = 'and' ): array { + global $wp_comment_types; + + $field = ( 'names' === $output ) ? 'name' : false; + + /** @var array|array $comment_types */ + $comment_types = wp_filter_object_list( $wp_comment_types, $args, $operator, $field ); + + return $comment_types; +} + +/** + * Adds the comment types registered as internal to the default-excluded comment types. + * + * Hooked to {@see 'default_excluded_comment_types'}, so registering a type as internal + * is enough to keep it out of comment queries, counts and feeds by default. + * + * @since 7.3.0 + * @access private + * + * @param string[] $excluded_types Comment types excluded by default. + * @return string[] Comment types excluded by default, including every internal type. + * + * @phpstan-return list + */ +function _wp_exclude_internal_comment_types( $excluded_types ): array { + if ( ! is_array( $excluded_types ) ) { + $excluded_types = array(); + } + $internal_types = get_comment_types( array( 'internal' => true ) ); + + return array_values( array_unique( array_merge( $excluded_types, $internal_types ) ) ); +} + +/** + * Determines whether a comment type is registered. + * + * @since 7.3.0 + * + * @param string $comment_type Comment type name. + * @return bool Whether the comment type is registered. + */ +function comment_type_exists( $comment_type ): bool { + return get_comment_type_object( $comment_type ) instanceof WP_Comment_Type; +} + +/** + * Builds an object with all comment type labels out of a comment type object. + * + * @since 7.3.0 + * + * @param WP_Comment_Type $comment_type_object Comment type object. + * @return stdClass { + * Comment type labels object. + * + * @type string $name General name for the comment type, usually plural. The same as and + * overridden by `$comment_type_object->label`. Default 'Comments'. + * @type string $singular_name Name for one object of this comment type. Default 'Comment'. + * @type string $menu_name Label for the menu name. Default is the same as `name`. + * } + */ +function get_comment_type_labels( $comment_type_object ): stdClass { + $defaults = array(); + + foreach ( WP_Comment_Type::get_default_labels() as $key => $value ) { + $defaults[ $key ] = $value[0]; + } + + $defaults['menu_name'] = $defaults['name']; + + $labels = (array) $comment_type_object->labels; + + if ( ! empty( $comment_type_object->label ) && empty( $labels['name'] ) ) { + $labels['name'] = $comment_type_object->label; + } + + if ( ! isset( $labels['singular_name'] ) && isset( $labels['name'] ) ) { + $labels['singular_name'] = $labels['name']; + } + + if ( ! isset( $labels['menu_name'] ) && isset( $labels['name'] ) ) { + $labels['menu_name'] = $labels['name']; + } + + $labels = (object) array_merge( $defaults, $labels ); + + $comment_type = $comment_type_object->name; + + $default_labels = clone $labels; + + /** + * Filters the labels of a specific comment type. + * + * The dynamic portion of the hook name, `$comment_type`, refers to the comment type slug. + * + * Possible hook names include: + * + * - `comment_type_labels_comment` + * - `comment_type_labels_pingback` + * + * Labels are stored unescaped, mirroring the post type and taxonomy label + * contract; callers must escape them on output (for example with esc_html()). + * + * @since 7.3.0 + * + * @see get_comment_type_labels() for the full list of comment type labels. + * + * @param object $labels Object with labels for the comment type as member variables. + */ + $labels = apply_filters( "comment_type_labels_{$comment_type}", $labels ); + + // Ensure that the filtered labels contain all required default values. + $labels = (object) array_merge( (array) $default_labels, (array) $labels ); + + return $labels; +} + /** * Retrieves all of the WordPress supported comment statuses. * diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 3c1234f9cfa8e..e4995bfef8789 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -542,6 +542,11 @@ add_action( 'split_shared_term', '_wp_check_split_nav_menu_terms', 10, 4 ); add_action( 'wp_split_shared_term_batch', '_wp_batch_split_terms' ); +// Comment types. +add_action( 'init', 'create_initial_comment_types', 0 ); // Early, like the initial post types. +add_action( 'change_locale', 'create_initial_comment_types' ); +add_filter( 'default_excluded_comment_types', '_wp_exclude_internal_comment_types', 0 ); // Early, so later callbacks filter the full set. + // Comment type updates. add_action( 'admin_init', '_wp_check_for_scheduled_update_comment_type' ); add_action( 'wp_update_comment_type_batch', '_wp_batch_update_comment_type' ); diff --git a/src/wp-settings.php b/src/wp-settings.php index a38eaccc56956..1a97387fd4be7 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -238,6 +238,7 @@ require ABSPATH . WPINC . '/comment.php'; require ABSPATH . WPINC . '/class-wp-comment.php'; require ABSPATH . WPINC . '/class-wp-comment-query.php'; +require ABSPATH . WPINC . '/class-wp-comment-type.php'; require ABSPATH . WPINC . '/class-walker-comment.php'; require ABSPATH . WPINC . '/comment-template.php'; require ABSPATH . WPINC . '/rewrite.php'; @@ -591,10 +592,11 @@ // Create common globals. require ABSPATH . WPINC . '/vars.php'; -// Make taxonomies and posts available to plugins and themes. +// Make taxonomies, posts, and comment types available to plugins and themes. // @plugin authors: warning: these get registered again on the init hook. create_initial_taxonomies(); create_initial_post_types(); +create_initial_comment_types(); wp_start_scraping_edited_file_errors(); diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index edae9d14c2035..80259382723b7 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -260,15 +260,16 @@ public function set_up() { $this->original_translation_locale = WP_Translation_Controller::get_instance()->get_locale(); /* - * When running core tests, ensure that post types and taxonomies - * are reset for each test. We skip this step for non-core tests, - * given the large number of plugins that register post types and - * taxonomies at 'init'. + * When running core tests, ensure that post types, taxonomies, + * and comment types are reset for each test. We skip this step + * for non-core tests, given the large number of plugins that + * register post types and taxonomies at 'init'. */ if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) { $this->reset_post_types(); $this->reset_taxonomies(); $this->reset_post_statuses(); + $this->reset_comment_types(); $this->reset__SERVER(); if ( WP_TESTS_DEFAULT_PERMALINK_STRUCTURE !== $wp_rewrite->permalink_structure ) { @@ -553,6 +554,22 @@ protected function reset_taxonomies() { create_initial_taxonomies(); } + /** + * Unregisters existing comment types and registers defaults. + * + * Run before each test in order to clean up the global scope, in case + * a test forgets to unregister a comment type on its own, or fails before + * it has a chance to do so. + * + * Empties the registry outright, since a custom type registered with '_builtin' + * cannot be unregistered. + */ + protected function reset_comment_types(): void { + $GLOBALS['wp_comment_types'] = array(); + + create_initial_comment_types(); + } + /** * Unregisters non-built-in post statuses. */ diff --git a/tests/phpunit/tests/comment/commentType.php b/tests/phpunit/tests/comment/commentType.php new file mode 100644 index 0000000000000..21380b07f4b0a --- /dev/null +++ b/tests/phpunit/tests/comment/commentType.php @@ -0,0 +1,166 @@ +post->create(); + } + + /** + * Returns the output of comment_type() for a comment of the given type. + * + * @param string $type Comment type stored on the comment. + * @param string ...$args Optional text arguments passed through to comment_type(). + * @return string Captured output. + */ + private function get_comment_type_output( string $type, string ...$args ): string { + $comment_id = self::factory()->comment->create( + array( + 'comment_post_ID' => self::$post_id, + 'comment_type' => $type, + ) + ); + + $GLOBALS['comment'] = get_comment( $comment_id ); + + ob_start(); + comment_type( ...$args ); + $output = ob_get_clean(); + + unset( $GLOBALS['comment'] ); + + $this->assertIsString( $output ); + + return $output; + } + + /** + * @ticket 35214 + */ + public function test_built_in_types_output_is_unchanged(): void { + $this->assertSame( 'Comment', $this->get_comment_type_output( 'comment' ) ); + $this->assertSame( 'Trackback', $this->get_comment_type_output( 'trackback' ) ); + $this->assertSame( 'Pingback', $this->get_comment_type_output( 'pingback' ) ); + } + + /** + * @ticket 35214 + */ + public function test_custom_text_overrides_are_respected(): void { + $this->assertSame( 'C', $this->get_comment_type_output( 'comment', 'C', 'T', 'P' ) ); + $this->assertSame( 'T', $this->get_comment_type_output( 'trackback', 'C', 'T', 'P' ) ); + $this->assertSame( 'P', $this->get_comment_type_output( 'pingback', 'C', 'T', 'P' ) ); + } + + /** + * @ticket 35214 + */ + public function test_registered_custom_type_outputs_its_label(): void { + register_comment_type( + 'foo', + array( + 'labels' => array( + 'singular_name' => 'Foo', + ), + ) + ); + + $this->assertSame( 'Foo', $this->get_comment_type_output( 'foo' ) ); + } + + /** + * @ticket 35214 + */ + public function test_unregistered_custom_type_falls_back_to_default_label(): void { + $this->assertSame( _x( 'Comment', 'noun' ), $this->get_comment_type_output( 'bar' ) ); + } + + /** + * A comment stored with the legacy empty string type is treated as 'comment'. + * + * @ticket 35214 + */ + public function test_legacy_empty_type_outputs_comment(): void { + $this->assertSame( 'Comment', $this->get_comment_type_output( '' ) ); + } + + /** + * Built-in types other than 'comment', 'pingback', and 'trackback' use their registered label. + * + * @ticket 35214 + */ + public function test_built_in_internal_types_output_their_label(): void { + $this->assertSame( 'Note', $this->get_comment_type_output( 'note' ) ); + $this->assertSame( 'Reaction', $this->get_comment_type_output( 'reaction' ) ); + } + + /** + * Overriding the 'comment' label does not change the default comment_type() output. + * + * @ticket 35214 + */ + public function test_comment_type_output_ignores_filtered_comment_label(): void { + add_filter( + 'comment_type_labels_comment', + static function ( stdClass $labels ): stdClass { + $labels->singular_name = 'Filtered'; + return $labels; + } + ); + + create_initial_comment_types(); + + $this->assertSame( 'Comment', $this->get_comment_type_output( 'comment' ) ); + } + + /** + * @ticket 35214 + */ + public function test_custom_text_override_wins_over_registered_label(): void { + register_comment_type( + 'foo', + array( + 'labels' => array( + 'singular_name' => 'Foo', + ), + ) + ); + + $this->assertSame( 'Custom', $this->get_comment_type_output( 'foo', 'Custom' ) ); + } + + /** + * The registered label is escaped on output to guard against HTML/script injection. + * + * @ticket 35214 + */ + public function test_registered_label_is_escaped_on_output(): void { + register_comment_type( + 'foo', + array( + 'labels' => array( + 'singular_name' => 'Foo', + ), + ) + ); + + $this->assertSame( + esc_html( 'Foo' ), + $this->get_comment_type_output( 'foo' ) + ); + } +} diff --git a/tests/phpunit/tests/comment/types.php b/tests/phpunit/tests/comment/types.php new file mode 100644 index 0000000000000..ebd1a1a7fdaa1 --- /dev/null +++ b/tests/phpunit/tests/comment/types.php @@ -0,0 +1,853 @@ +assertInstanceOf( WP_Comment_Type::class, $comment_type_object, "The {$comment_type} comment type should be registered." ); + return $comment_type_object; + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + * @covers ::get_comment_type_object + */ + public function test_register_comment_type(): void { + $before = get_comment_type_object( 'foo' ); + $this->assertNull( $before ); + + register_comment_type( 'foo' ); + + $cobj = get_comment_type_object( 'foo' ); + $this->assertInstanceOf( 'WP_Comment_Type', $cobj ); + $this->assertSame( 'foo', $cobj->name ); + + // Test some defaults. + $this->assertTrue( $cobj->public ); + $this->assertFalse( $cobj->internal ); + $this->assertFalse( $cobj->_builtin ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + * @covers ::get_comment_type_labels + */ + public function test_register_comment_type_without_labels_uses_default_labels(): void { + register_comment_type( 'foo' ); + + $cobj = $this->get_registered_comment_type( 'foo' ); + + $this->assertSame( 'Comments', $cobj->label ); + $this->assertSame( 'Comments', $cobj->labels->name ); + $this->assertSame( 'Comment', $cobj->labels->singular_name ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + */ + public function test_register_comment_type_return_value(): void { + $this->assertInstanceOf( 'WP_Comment_Type', register_comment_type( 'foo' ) ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + * + * @expectedIncorrectUsage register_comment_type + */ + public function test_register_comment_type_with_too_long_name(): void { + $this->assertInstanceOf( 'WP_Error', register_comment_type( 'comment_type_with_a_too_long_name' ) ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + * + * @expectedIncorrectUsage register_comment_type + */ + public function test_register_comment_type_with_empty_name(): void { + $this->assertInstanceOf( 'WP_Error', register_comment_type( '' ) ); + } + + /** + * wp_insert_comment() stores an empty() type as 'comment', so '0' cannot be registered. + * + * @ticket 35214 + * + * @covers ::register_comment_type + * + * @expectedIncorrectUsage register_comment_type + */ + public function test_register_comment_type_with_zero_name_is_rejected(): void { + $result = register_comment_type( '0' ); + + $this->assertWPError( $result ); + $this->assertSame( 'comment_type_name_invalid', $result->get_error_code() ); + $this->assertFalse( comment_type_exists( '0' ) ); + } + + /** + * @ticket 35214 + * + * @covers ::create_initial_comment_types + */ + public function test_built_in_comment_types_are_registered(): void { + $this->assertTrue( comment_type_exists( 'comment' ) ); + $this->assertTrue( comment_type_exists( 'pingback' ) ); + $this->assertTrue( comment_type_exists( 'trackback' ) ); + $this->assertTrue( comment_type_exists( 'note' ) ); + $this->assertTrue( comment_type_exists( 'reaction' ) ); + } + + /** + * @ticket 35214 + * + * @covers ::create_initial_comment_types + */ + public function test_built_in_note_type_is_internal_and_non_public(): void { + $note = $this->get_registered_comment_type( 'note' ); + + $this->assertTrue( $note->internal ); + $this->assertFalse( $note->public ); + } + + /** + * @ticket 35214 + * + * @covers ::create_initial_comment_types + */ + public function test_built_in_reaction_type_is_internal_and_non_public(): void { + $reaction = $this->get_registered_comment_type( 'reaction' ); + + $this->assertTrue( $reaction->internal ); + $this->assertFalse( $reaction->public ); + $this->assertTrue( $reaction->_builtin ); + $this->assertSame( 'Reactions', $reaction->label ); + $this->assertSame( 'Reaction', $reaction->labels->singular_name ); + } + + /** + * @ticket 35214 + * + * @covers ::comment_type_exists + */ + public function test_comment_type_exists(): void { + $this->assertFalse( comment_type_exists( 'foo' ) ); + + register_comment_type( 'foo' ); + + $this->assertTrue( comment_type_exists( 'foo' ) ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_types + */ + public function test_get_comment_types_names(): void { + register_comment_type( 'foo' ); + + $types = get_comment_types(); + + $this->assertContains( 'comment', $types ); + $this->assertContains( 'foo', $types ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_types + */ + public function test_get_comment_types_objects(): void { + register_comment_type( 'foo' ); + + $types = get_comment_types( array(), 'objects' ); + + $this->assertInstanceOf( 'WP_Comment_Type', $types['foo'] ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_types + */ + public function test_get_comment_types_filtered_by_property(): void { + register_comment_type( 'foo', array( 'public' => false ) ); + + $public = get_comment_types( array( 'public' => true ) ); + + $this->assertContains( 'comment', $public ); + $this->assertNotContains( 'foo', $public ); + $this->assertNotContains( 'note', $public ); + $this->assertNotContains( 'reaction', $public ); + } + + /** + * Registering a type as internal is enough to exclude it by default. + * + * @ticket 35214 + * @ticket 65537 + * + * @covers ::_wp_exclude_internal_comment_types + */ + public function test_internal_comment_types_are_added_to_the_default_excluded_types(): void { + register_comment_type( 'foo', array( 'internal' => true ) ); + register_comment_type( 'bar' ); + + $this->assertSame( + array( 'note', 'reaction', 'foo' ), + _wp_exclude_internal_comment_types( array( 'note', 'reaction' ) ), + 'Only the internal types should be added, without repeating the built-in ones.' + ); + } + + /** + * The callback runs ahead of the default priority, so a plugin filtering the + * excluded types sees the internal ones and has the last word on them. + * + * @ticket 35214 + * @ticket 65537 + */ + public function test_internal_comment_types_are_hooked_before_the_default_priority(): void { + $this->assertSame( 0, has_filter( 'default_excluded_comment_types', '_wp_exclude_internal_comment_types' ) ); + } + + /** + * @ticket 35214 + * + * @covers ::unregister_comment_type + */ + public function test_unregister_comment_type(): void { + register_comment_type( 'foo' ); + + $this->assertTrue( unregister_comment_type( 'foo' ) ); + $this->assertNull( get_comment_type_object( 'foo' ) ); + } + + /** + * @ticket 35214 + * + * @covers ::unregister_comment_type + */ + public function test_unregister_comment_type_unknown_returns_error(): void { + $this->assertWPError( unregister_comment_type( 'does_not_exist' ) ); + } + + /** + * @ticket 35214 + * + * @covers ::unregister_comment_type + */ + public function test_unregister_comment_type_twice_returns_error(): void { + register_comment_type( 'foo' ); + + $this->assertTrue( unregister_comment_type( 'foo' ) ); + $this->assertWPError( unregister_comment_type( 'foo' ) ); + } + + /** + * @ticket 35214 + * + * @covers ::unregister_comment_type + * + * @dataProvider data_built_in_comment_types + */ + public function test_unregister_built_in_comment_type_is_not_allowed( string $comment_type ): void { + $this->assertWPError( unregister_comment_type( $comment_type ) ); + $this->assertTrue( comment_type_exists( $comment_type ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_built_in_comment_types(): array { + return array( + array( 'comment' ), + array( 'pingback' ), + array( 'trackback' ), + array( 'note' ), + array( 'reaction' ), + ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + * + * @expectedIncorrectUsage register_comment_type + * + * @dataProvider data_built_in_comment_types + */ + public function test_register_built_in_comment_type_is_rejected( string $comment_type ): void { + $original_label = $this->get_registered_comment_type( $comment_type )->label; + + $result = register_comment_type( $comment_type, array( 'label' => 'Hijacked' ) ); + + $this->assertWPError( $result ); + $this->assertSame( 'comment_type_builtin', $result->get_error_code() ); + $this->assertSame( $original_label, $this->get_registered_comment_type( $comment_type )->label ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + */ + public function test_register_comment_type_twice_overwrites_previous_registration(): void { + register_comment_type( 'foo', array( 'label' => 'First' ) ); + register_comment_type( 'foo', array( 'label' => 'Second' ) ); + + $this->assertSame( 'Second', $this->get_registered_comment_type( 'foo' )->label ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + * @covers ::unregister_comment_type + */ + public function test_register_after_unregister_succeeds(): void { + register_comment_type( 'foo' ); + unregister_comment_type( 'foo' ); + + $this->assertInstanceOf( 'WP_Comment_Type', register_comment_type( 'foo' ) ); + $this->assertTrue( comment_type_exists( 'foo' ) ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + */ + public function test_registered_comment_type_actions_fire(): void { + $action = new MockAction(); + $action_for_foo = new MockAction(); + + add_action( 'registered_comment_type', array( $action, 'action' ) ); + add_action( 'registered_comment_type_foo', array( $action_for_foo, 'action' ) ); + + register_comment_type( 'foo' ); + + $this->assertSame( 1, $action->get_call_count() ); + $this->assertSame( 1, $action_for_foo->get_call_count() ); + } + + /** + * @ticket 35214 + * + * @covers ::unregister_comment_type + */ + public function test_unregistered_comment_type_action_fires(): void { + register_comment_type( 'foo' ); + + $action = new MockAction(); + add_action( 'unregistered_comment_type', array( $action, 'action' ) ); + + unregister_comment_type( 'foo' ); + + $this->assertSame( 1, $action->get_call_count() ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + */ + public function test_register_comment_type_with_20_character_name_succeeds(): void { + $comment_type = str_repeat( 'a', 20 ); + + $this->assertInstanceOf( 'WP_Comment_Type', register_comment_type( $comment_type ) ); + $this->assertTrue( comment_type_exists( $comment_type ) ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + * + * @expectedIncorrectUsage register_comment_type + * + * @dataProvider data_comment_type_names_changed_by_sanitize_key + */ + public function test_register_comment_type_name_that_sanitize_key_would_change_is_rejected( string $comment_type ): void { + $result = register_comment_type( $comment_type ); + + $this->assertWPError( $result ); + $this->assertSame( 'comment_type_name_invalid', $result->get_error_code() ); + $this->assertFalse( comment_type_exists( $comment_type ) ); + $this->assertFalse( comment_type_exists( sanitize_key( $comment_type ) ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_comment_type_names_changed_by_sanitize_key(): array { + return array( + 'uppercase' => array( 'Review' ), + 'spaces and symbols' => array( 'Foo Bar!' ), + ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_types + */ + public function test_get_comment_types_with_or_operator(): void { + register_comment_type( 'foo', array( 'public' => false ) ); + + $types = get_comment_types( + array( + 'public' => true, + 'internal' => true, + ), + 'names', + 'or' + ); + + // 'comment' matches on public, 'note' matches on internal. + $this->assertContains( 'comment', $types ); + $this->assertContains( 'note', $types ); + $this->assertNotContains( 'foo', $types ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_types + */ + public function test_get_comment_types_with_not_operator(): void { + register_comment_type( 'foo', array( 'internal' => true ) ); + + $types = get_comment_types( array( 'internal' => true ), 'names', 'not' ); + + $this->assertContains( 'comment', $types ); + $this->assertNotContains( 'note', $types ); + $this->assertNotContains( 'foo', $types ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_types + */ + public function test_get_comment_types_names_output_is_keyed_by_type_name(): void { + register_comment_type( 'foo' ); + + $types = get_comment_types(); + + $this->assertSame( 'foo', $types['foo'] ); + $this->assertSame( 'comment', $types['comment'] ); + } + + /** + * @ticket 35214 + * + * @covers ::create_initial_comment_types + */ + public function test_create_initial_comment_types_is_idempotent(): void { + create_initial_comment_types(); + create_initial_comment_types(); + + $this->assertCount( 5, get_comment_types( array( '_builtin' => true ) ) ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_type_labels + */ + public function test_labels_are_built_from_args(): void { + register_comment_type( + 'foo', + array( + 'label' => 'Foos', + 'labels' => array( + 'singular_name' => 'Foo', + ), + ) + ); + + $cobj = $this->get_registered_comment_type( 'foo' ); + + $this->assertSame( 'Foos', $cobj->label ); + $this->assertSame( 'Foos', $cobj->labels->name ); + $this->assertSame( 'Foo', $cobj->labels->singular_name ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_type_labels + */ + public function test_comment_type_labels_filter(): void { + add_filter( + 'comment_type_labels_foo', + static function ( stdClass $labels ): stdClass { + $labels->singular_name = 'Filtered Foo'; + return $labels; + } + ); + + register_comment_type( 'foo' ); + + $this->assertSame( 'Filtered Foo', $this->get_registered_comment_type( 'foo' )->labels->singular_name ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_type_labels + */ + public function test_label_only_registration_populates_label_fallback_chain(): void { + register_comment_type( 'foo', array( 'label' => 'Foos' ) ); + + $labels = $this->get_registered_comment_type( 'foo' )->labels; + + $this->assertSame( 'Foos', $labels->name ); + $this->assertSame( 'Foos', $labels->singular_name ); + $this->assertSame( 'Foos', $labels->menu_name ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_type_labels + */ + public function test_labels_do_not_include_post_type_only_labels(): void { + register_comment_type( 'foo', array( 'label' => 'Foos' ) ); + + $labels = $this->get_registered_comment_type( 'foo' )->labels; + + $this->assertObjectNotHasProperty( 'name_admin_bar', $labels ); + $this->assertObjectNotHasProperty( 'all_items', $labels ); + $this->assertObjectNotHasProperty( 'archives', $labels ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_type_labels + */ + public function test_get_comment_type_labels_does_not_modify_a_registered_type(): void { + register_comment_type( 'foo', array( 'label' => 'Foos' ) ); + + $comment_type_object = $this->get_registered_comment_type( 'foo' ); + $registered_labels = get_object_vars( $comment_type_object->labels ); + + $first = get_comment_type_labels( $comment_type_object ); + $second = get_comment_type_labels( $comment_type_object ); + + $this->assertSame( $registered_labels, get_object_vars( $comment_type_object->labels ), 'The registered labels should not change.' ); + $this->assertEquals( $first, $second, 'Repeated calls should return the same labels.' ); + $this->assertObjectNotHasProperty( 'archives', $second, 'Post-type-only labels should not appear on repeated calls.' ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_type_labels + */ + public function test_labels_do_not_spawn_post_type_only_labels_from_menu_name(): void { + register_comment_type( + 'foo', + array( + 'label' => 'Foos', + 'labels' => array( + 'menu_name' => 'Foo Menu', + ), + ) + ); + + $labels = $this->get_registered_comment_type( 'foo' )->labels; + + $this->assertSame( 'Foo Menu', $labels->menu_name ); + $this->assertObjectNotHasProperty( 'all_items', $labels ); + $this->assertObjectNotHasProperty( 'archives', $labels ); + } + + /** + * @ticket 35214 + * + * @covers ::get_comment_type_labels + */ + public function test_comment_type_labels_filter_missing_name_is_backfilled(): void { + add_filter( + 'comment_type_labels_foo', + static function ( stdClass $labels ): stdClass { + unset( $labels->name ); + return $labels; + } + ); + + register_comment_type( 'foo', array( 'label' => 'Foos' ) ); + + $this->assertSame( 'Foos', $this->get_registered_comment_type( 'foo' )->labels->name ); + } + + /** + * @ticket 35214 + * + * @covers ::register_comment_type + */ + public function test_registered_comment_type_action_receives_type_and_object(): void { + $action = new MockAction(); + + add_action( 'registered_comment_type', array( $action, 'action' ), 10, 2 ); + + register_comment_type( 'foo' ); + + $args = $action->get_args(); + + $this->assertSame( 'foo', $args[0][0] ); + $this->assertInstanceOf( 'WP_Comment_Type', $args[0][1] ); + $this->assertSame( 'foo', $args[0][1]->name ); + } + + /** + * Comment types are never hierarchical, so a 'hierarchical' argument has no effect + * on the labels. + * + * @ticket 35214 + * + * @covers WP_Comment_Type::set_props + */ + public function test_register_comment_type_ignores_hierarchical_argument(): void { + register_comment_type( 'foo', array( 'hierarchical' => true ) ); + + $cobj = $this->get_registered_comment_type( 'foo' ); + + $this->assertSame( 'Comments', $cobj->label, 'The default label should survive the argument.' ); + $this->assertSame( 'Comments', $cobj->labels->name ); + $this->assertSame( 'Comment', $cobj->labels->singular_name ); + } + + /** + * The names WP_Comment_Query reads as query tokens cannot be registered, since a type + * stored under one of them could never be queried for on its own. + * + * @ticket 35214 + * + * @covers ::register_comment_type + * + * @dataProvider data_reserved_comment_type_names + * + * @expectedIncorrectUsage register_comment_type + * + * @param string $comment_type Reserved comment type name. + */ + public function test_register_reserved_comment_type_is_rejected( string $comment_type ): void { + $result = register_comment_type( $comment_type ); + + $this->assertInstanceOf( 'WP_Error', $result ); + $this->assertSame( 'comment_type_reserved', $result->get_error_code() ); + $this->assertNull( get_comment_type_object( $comment_type ) ); + } + + /** + * Data provider for test_register_reserved_comment_type_is_rejected(). + * + * @return array + */ + public function data_reserved_comment_type_names(): array { + return array( + 'all type token' => array( 'all' ), + 'comments alias' => array( 'comments' ), + 'pings bucket key' => array( 'pings' ), + ); + } + + /** + * Passing '_builtin' does not get around the built-in guard. + * + * @ticket 35214 + * + * @covers ::register_comment_type + * + * @expectedIncorrectUsage register_comment_type + */ + public function test_register_comment_type_builtin_argument_does_not_bypass_the_built_in_guard(): void { + $result = register_comment_type( + 'note', + array( + '_builtin' => true, + 'label' => 'Hijacked', + 'internal' => false, + ) + ); + + $this->assertWPError( $result ); + $this->assertSame( 'comment_type_builtin', $result->get_error_code() ); + + $note = $this->get_registered_comment_type( 'note' ); + + $this->assertNotSame( 'Hijacked', $note->label ); + $this->assertTrue( $note->internal ); + } + + /** + * The args filters cannot remove built-in status from a core type. + * + * @ticket 35214 + * + * @covers WP_Comment_Type::set_props + */ + public function test_args_filter_cannot_remove_built_in_status(): void { + add_filter( + 'register_comment_comment_type_args', + static function ( array $args ): array { + $args['_builtin'] = false; + return $args; + } + ); + + create_initial_comment_types(); + + $this->assertTrue( $this->get_registered_comment_type( 'comment' )->_builtin ); + $this->assertWPError( unregister_comment_type( 'comment' ) ); + } + + /** + * The args filters cannot lock a custom type by marking it built-in. + * + * @ticket 35214 + * + * @covers WP_Comment_Type::set_props + */ + public function test_args_filter_cannot_add_built_in_status(): void { + add_filter( + 'register_comment_type_args', + static function ( array $args ): array { + $args['_builtin'] = true; + return $args; + } + ); + + register_comment_type( 'foo' ); + + $this->assertFalse( $this->get_registered_comment_type( 'foo' )->_builtin ); + $this->assertTrue( unregister_comment_type( 'foo' ) ); + } + + /** + * Passing '_builtin' on a custom type makes it behave like a built-in for everyone + * else: it can no longer be re-registered or unregistered. + * + * @ticket 35214 + * + * @covers ::register_comment_type + * @covers ::unregister_comment_type + * + * @expectedIncorrectUsage register_comment_type + */ + public function test_register_comment_type_builtin_argument_locks_a_custom_type(): void { + register_comment_type( 'foo', array( '_builtin' => true ) ); + + $this->assertInstanceOf( 'WP_Error', register_comment_type( 'foo' ) ); + $this->assertInstanceOf( 'WP_Error', unregister_comment_type( 'foo' ) ); + } + + /** + * Built-in labels are rebuilt on a locale change rather than served from the static + * default-labels cache. + * + * @ticket 35214 + * + * @covers ::create_initial_comment_types + */ + public function test_built_in_labels_are_rebuilt_on_locale_change(): void { + $original = $this->get_registered_comment_type( 'comment' )->label; + + add_filter( + 'gettext', + static function ( $translation, $text ) { + return 'Comments' === $text ? 'Kommentare' : $translation; + }, + 10, + 2 + ); + + /** This action is documented in wp-includes/class-wp-locale-switcher.php */ + do_action( 'change_locale', 'de_DE' ); + + $this->assertSame( + 'Kommentare', + $this->get_registered_comment_type( 'comment' )->label, + 'A locale change should rebuild the built-in labels.' + ); + $this->assertNotSame( $original, $this->get_registered_comment_type( 'comment' )->label ); + } + + /** + * Before wp-settings.php registers the built-ins, the registry global does not exist. + * Both accessors have to cope with that rather than warn. + * + * @ticket 35214 + * + * @covers ::get_comment_types + * @covers ::get_comment_type_object + */ + public function test_accessors_handle_an_unset_registry(): void { + $registry = $GLOBALS['wp_comment_types']; + unset( $GLOBALS['wp_comment_types'] ); + + try { + $this->assertSame( array(), get_comment_types() ); + $this->assertNull( get_comment_type_object( 'comment' ) ); + } finally { + $GLOBALS['wp_comment_types'] = $registry; + } + } + + /** + * The registry is a per-process global, so it is not scoped to a site. This matches + * post types, and is worth pinning because comment counts and query exclusions are + * per-site data. + * + * @ticket 35214 + * + * @group ms-required + * + * @covers ::register_comment_type + */ + public function test_registry_is_not_scoped_to_a_site(): void { + register_comment_type( 'foo' ); + + $blog_id = self::factory()->blog->create(); + + switch_to_blog( $blog_id ); + + $registered_after_switch = comment_type_exists( 'foo' ); + + restore_current_blog(); + + $this->assertTrue( + $registered_after_switch, + 'A registered comment type should still be registered after switch_to_blog().' + ); + } +} diff --git a/tests/phpunit/tests/comment/wpCommentType.php b/tests/phpunit/tests/comment/wpCommentType.php new file mode 100644 index 0000000000000..b2c873817f8d3 --- /dev/null +++ b/tests/phpunit/tests/comment/wpCommentType.php @@ -0,0 +1,151 @@ +assertSame( 'foo', $comment_type->name ); + $this->assertTrue( $comment_type->public ); + $this->assertFalse( $comment_type->internal ); + $this->assertFalse( $comment_type->_builtin ); + } + + /** + * @ticket 35214 + * + * @covers ::set_props + */ + public function test_set_props_overrides_defaults(): void { + $comment_type = new WP_Comment_Type( + 'foo', + array( + 'public' => false, + 'internal' => true, + 'description' => 'A test comment type.', + ) + ); + + $this->assertFalse( $comment_type->public ); + $this->assertTrue( $comment_type->internal ); + $this->assertSame( 'A test comment type.', $comment_type->description ); + } + + /** + * @ticket 35214 + * + * @covers ::set_props + */ + public function test_register_comment_type_args_filter(): void { + $filter = static function ( array $args ): array { + $args['public'] = false; + return $args; + }; + + add_filter( 'register_comment_type_args', $filter ); + $comment_type = new WP_Comment_Type( 'foo' ); + remove_filter( 'register_comment_type_args', $filter ); + + $this->assertFalse( $comment_type->public ); + } + + /** + * @ticket 35214 + * + * @covers ::set_props + */ + public function test_register_specific_comment_type_args_filter(): void { + $filter = static function ( array $args ): array { + $args['description'] = 'Filtered description.'; + return $args; + }; + + add_filter( 'register_foo_comment_type_args', $filter ); + $comment_type = new WP_Comment_Type( 'foo' ); + $other_type = new WP_Comment_Type( 'bar' ); + remove_filter( 'register_foo_comment_type_args', $filter ); + + $this->assertSame( 'Filtered description.', $comment_type->description ); + $this->assertSame( '', $other_type->description ); + } + + /** + * The properties are typed, so filtered arguments of the wrong type must not cause a TypeError. + * + * @ticket 35214 + * + * @covers ::set_props + */ + public function test_set_props_normalizes_filtered_arguments_of_the_wrong_type(): void { + $filter = static function ( array $args ): array { + $args['label'] = array( 'Not a string' ); + $args['labels'] = 'Not an array'; + $args['description'] = null; + $args['public'] = 0; + $args['internal'] = '1'; + return $args; + }; + + add_filter( 'register_comment_type_args', $filter ); + $comment_type = new WP_Comment_Type( 'foo' ); + remove_filter( 'register_comment_type_args', $filter ); + + $this->assertSame( 'Comments', $comment_type->label ); + $this->assertSame( 'Comment', $comment_type->labels->singular_name ); + $this->assertSame( '', $comment_type->description ); + $this->assertFalse( $comment_type->public ); + $this->assertTrue( $comment_type->internal ); + } + + /** + * @ticket 35214 + * + * @covers ::get_default_labels + * @covers ::reset_default_labels + */ + public function test_get_default_labels_returns_expected_defaults(): void { + WP_Comment_Type::reset_default_labels(); + + $labels = WP_Comment_Type::get_default_labels(); + + $this->assertSame( 'Comments', $labels['name'][0] ); + $this->assertSame( 'Comment', $labels['singular_name'][0] ); + } + + /** + * @ticket 35214 + * + * @covers ::get_default_labels + * @covers ::reset_default_labels + */ + public function test_reset_default_labels_clears_cache(): void { + // Poison the static cache so a stale value is observable. + $property = new ReflectionProperty( WP_Comment_Type::class, 'default_labels' ); + if ( PHP_VERSION_ID < 80100 ) { + $property->setAccessible( true ); + } + $property->setValue( null, array( 'name' => array( 'Poisoned', null ) ) ); + + $labels = WP_Comment_Type::get_default_labels(); + $this->assertSame( 'Poisoned', $labels['name'][0], 'The poisoned cache should be served as-is.' ); + + WP_Comment_Type::reset_default_labels(); + + // A fresh call rebuilds the defaults from translation functions. + $labels = WP_Comment_Type::get_default_labels(); + $this->assertSame( 'Comments', $labels['name'][0], 'Resetting should rebuild the default labels.' ); + } +}