Comments: Add optional $context parameter to get_edit_comment_link() to get the URL without HTML entities.

This brings the function in line with the similar `get_edit_post_link()` parameter. The 'get_edit_comment_link' filter now additionally receives the `$comment_id` and `$context` as parameters.

Additionally, as a minor enhancement, the capability check is now more defensive, as it will no longer cause an error if the given comment ID is invalid.

As part of the changeset, comprehensive test coverage for the `get_edit_comment_link()` including the new behavior is added.

Props deepakrohilla.
Fixes #61727.

Built from https://develop.svn.wordpress.org/trunk@58875


git-svn-id: http://core.svn.wordpress.org/trunk@58271 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2024-08-09 18:01:19 +00:00
parent ddab80be2c
commit 3fdbfb0f4a
2 changed files with 19 additions and 7 deletions

View File

@ -1595,27 +1595,39 @@ function get_delete_post_link( $post = 0, $deprecated = '', $force_delete = fals
* Retrieves the edit comment link.
*
* @since 2.3.0
* @since 6.7.0 The $context parameter was added.
*
* @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object.
* @return string|void The edit comment link URL for the given comment.
* @param string $context Optional. Context in which the URL should be used. Either 'display',
* to include HTML entities, or 'url'. Default 'display'.
* @return string|void The edit comment link URL for the given comment, or void if the comment id does not exist or
* the current user is not allowed to edit it.
*/
function get_edit_comment_link( $comment_id = 0 ) {
function get_edit_comment_link( $comment_id = 0, $context = 'display' ) {
$comment = get_comment( $comment_id );
if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
if ( ! is_object( $comment ) || ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
return;
}
$location = admin_url( 'comment.php?action=editcomment&c=' ) . $comment->comment_ID;
if ( 'display' === $context ) {
$action = 'comment.php?action=editcomment&c=';
} else {
$action = 'comment.php?action=editcomment&c=';
}
$location = admin_url( $action ) . $comment->comment_ID;
/**
* Filters the comment edit link.
*
* @since 2.3.0
* @since 6.7.0 The $comment_id and $context parameters are now being passed to the filter.
*
* @param string $location The edit link.
* @param int $comment_id Optional. Unique ID of the comment to generate an edit link.
* @param int $context Optional. Context to include HTML entities in link. Default 'display'.
*/
return apply_filters( 'get_edit_comment_link', $location );
return apply_filters( 'get_edit_comment_link', $location, $comment_id, $context );
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.7-alpha-58873';
$wp_version = '6.7-alpha-58875';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.