Thanks to visit codestin.com
Credit goes to github.com

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
243 changes: 243 additions & 0 deletions src/wp-includes/class-wp-comment-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
<?php
/**
* Comment API: WP_Comment_Type class
*
* @package WordPress
* @subpackage Comments
* @since 7.1.0
*/

/**
* Core class used for interacting with comment types.
*
* @since 7.1.0
*
* @see register_comment_type()
*/
#[AllowDynamicProperties]
final class WP_Comment_Type {
/**
* Comment type key.
*
* @since 7.1.0
* @var string
*/
public $name;

/**
* Name of the comment type shown in the menu. Usually plural.
*
* @since 7.1.0
* @var string
*/
public $label;

/**
* Labels object for this comment type.
*
* If not set, comment labels are inherited.
*
* @see get_comment_type_labels()
*
* @since 7.1.0
* @var stdClass
*/
public $labels;

/**
* Default labels.
*
* @since 7.1.0
* @var (string|null)[][] $default_labels
*/
protected static $default_labels = array();

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

/**
* Whether a comment type is intended for use publicly either via the admin interface or by front-end users.
*
* Default true.
*
* @since 7.1.0
* @var bool
*/
public $public = true;

/**
* Whether the comment type is for internal use only.
*
* Internal comment types (such as `note`) are excluded from default comment listings, counts,
* and other public-facing contexts. This is advisory metadata; the query layer is not affected
* by this property in this release.
*
* Default false.
*
* @since 7.1.0
* @var bool
*/
public $internal = false;

/**
* Whether to generate and allow a UI for managing this comment type in the admin.
*
* Default is the value of $public.
*
* @since 7.1.0
* @var bool
*/
public $show_ui;

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

/**
* Whether the comment type is hierarchical.
*
* Comment types are never hierarchical. This property exists so the shared
* label helper {@see _get_custom_object_labels()} can resolve default labels.
*
* @since 7.1.0
* @var bool
*/
public $hierarchical = 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.1.0
*
* @see register_comment_type()
*
* @param string $comment_type Comment type key.
* @param array|string $args Optional. Array or string of arguments for registering a comment type.
* Default empty array.
*/
public function __construct( $comment_type, $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.1.0
*
* @param array|string $args Array or string of arguments for registering a comment type.
*/
public function set_props( $args ) {
$args = wp_parse_args( $args );

/**
* Filters the arguments for registering a comment type.
*
* @since 7.1.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.
*/
$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.
*
* Possible hook names include:
*
* - `register_comment_comment_type_args`
* - `register_pingback_comment_type_args`
*
* @since 7.1.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.
*/
$args = apply_filters( "register_{$comment_type}_comment_type_args", $args, $this->name );

/*
* Note: 'label' is intentionally omitted from the defaults. Leaving the property
* unset (null) lets get_comment_type_labels() fall back to the default labels, the
* same way WP_Post_Type and WP_Taxonomy behave. A 'label' default of false would be
* treated as a provided value and overwrite the default name with false.
*/
$defaults = array(
'labels' => array(),
'description' => '',
'public' => true,
'internal' => false,
'show_ui' => null,
'_builtin' => false,
);

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

// If not set, default to the setting for 'public'.
if ( null === $args['show_ui'] ) {
$args['show_ui'] = $args['public'];
}

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

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.1.0
*
* @return (string|null)[][] The default labels for comment types.
*/
public static function get_default_labels() {
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.1.0
*/
public static function reset_default_labels() {
self::$default_labels = array();
}
}
16 changes: 15 additions & 1 deletion src/wp-includes/comment-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,9 @@ function get_comment_type( $comment_id = 0 ) {
* @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 ) {
// Whether the caller supplied custom text for the default comment label.
$comment_text_overridden = ( false !== $comment_text );

if ( false === $comment_text ) {
$comment_text = _x( 'Comment', 'noun' );
}
Expand All @@ -1203,7 +1206,18 @@ function comment_type( $comment_text = false, $trackback_text = false, $pingback
echo $pingback_text;
break;
default:
echo $comment_text;
/*
* For a registered, non-built-in comment type, fall back to its singular label
* when the caller did not supply custom text. Built-in types and explicit
* overrides keep their existing output.
*/
$comment_type_object = $comment_text_overridden ? null : get_comment_type_object( $type );

if ( $comment_type_object && ! $comment_type_object->_builtin && isset( $comment_type_object->labels->singular_name ) ) {
echo esc_html( $comment_type_object->labels->singular_name );
} else {
echo $comment_text;
}
}
}

Expand Down
Loading
Loading