Make WordPress Core

Changeset 58875


Ignore:
Timestamp:
08/09/2024 05:59:41 PM (4 months ago)
Author:
flixos90
Message:

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.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r58822 r58875  
    15961596 *
    15971597 * @since 2.3.0
     1598 * @since 6.7.0 The $context parameter was added.
    15981599 *
    15991600 * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object.
    1600  * @return string|void The edit comment link URL for the given comment.
    1601  */
    1602 function get_edit_comment_link( $comment_id = 0 ) {
     1601 * @param string         $context    Optional. Context in which the URL should be used. Either 'display',
     1602 *                                   to include HTML entities, or 'url'. Default 'display'.
     1603 * @return string|void The edit comment link URL for the given comment, or void if the comment id does not exist or
     1604 *                     the current user is not allowed to edit it.
     1605 */
     1606function get_edit_comment_link( $comment_id = 0, $context = 'display' ) {
    16031607    $comment = get_comment( $comment_id );
    16041608
    1605     if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
     1609    if ( ! is_object( $comment ) || ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
    16061610        return;
    16071611    }
    16081612
    1609     $location = admin_url( 'comment.php?action=editcomment&c=' ) . $comment->comment_ID;
     1613    if ( 'display' === $context ) {
     1614        $action = 'comment.php?action=editcomment&c=';
     1615    } else {
     1616        $action = 'comment.php?action=editcomment&c=';
     1617    }
     1618
     1619    $location = admin_url( $action ) . $comment->comment_ID;
    16101620
    16111621    /**
    16121622     * Filters the comment edit link.
    16131623     *
    1614      * @since 2.3.0
     1624     * @since 6.7.0 The $comment_id and $context parameters are now being passed to the filter.
    16151625     *
    16161626     * @param string $location The edit link.
    1617      */
    1618     return apply_filters( 'get_edit_comment_link', $location );
     1627     * @param int    $comment_id Optional. Unique ID of the comment to generate an edit link.
     1628     * @param int    $context    Optional. Context to include HTML entities in link. Default 'display'.
     1629     */
     1630    return apply_filters( 'get_edit_comment_link', $location, $comment_id, $context );
    16191631}
    16201632
Note: See TracChangeset for help on using the changeset viewer.