WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#12217 closed defect (bug) (fixed)

get_comment_reply_link gets post ID from wrong place

Reported by: juggledad Owned by:
Milestone: 3.0 Priority: normal
Severity: normal Version: 2.9.1
Component: General Keywords:
Focuses: Cc:

Description

I've been tracking down an issue with comments. In the Atahualpa theme, you can have comments on pages, but it only allows a reply on the first comment.

Digging my way thru the code, I came to function get_comment_reply_link and there are two lines that I wonder about. They are right after the 'extract($args, EXTR_SKIP);' and are:

$comment = get_comment($comment);
$post = get_post($post);

now it seems to me that the second line should be

$comment = get_comment($comment);
$post = get_post($comment->comment_post_ID);

i.e. grab the post from the post pointed to by the comment. You want to walk from teh comment to the post that it is pointed to, not assume that you have the post already.

Am I missing something? (p.s. this fixes my issue and allows all page comments to be replied to.

Change History (7)

comment:1 @dd326 years ago

the post_id should be retrieved from the passed $post to the function. In the event that its left at null, I'm pretty sure it should return the current post/page/object you're viewing.

You'll probably have to look a bit higher in the process to find out why $post is not being passed correctly.

comment:2 @juggledad6 years ago

The documentation in the code says;
/

  • Retrieve HTML content for reply to comment link. *
  • The default arguments that can be override are 'add_below', 'respond_id',
  • 'reply_text', 'login_text', and 'depth'. The 'login_text' argument will be
  • used, if the user must log in or register first before posting a comment. The
  • 'reply_text' will be used, if they can post a reply. The 'add_below' and
  • 'respond_id' arguments are for the JavaScript moveAddCommentForm() function
  • parameters. *
  • @since 2.7.0 *
  • @param array $args Optional. Override default options.
  • @param int $comment Optional. Comment being replied to.
  • @param int $post Optional. Post that the comment is going to be displayed on.
  • @return string|bool|null Link to show comment form, if successful. False, if comments are closed. */

Note that the parameter '$post' is OPTIONAL. Therefor the only way to gaurentee you can find the correct post id is to use the code suggested above.

comment:3 @nacin6 years ago

get_comment() and get_post() use the $comment and $post globals when they're passed null. Can you explain more what you're trying to do?

comment:4 @dd326 years ago

  • Milestone changed from Unassigned to 3.0

It does make sense to use the comment_post_ID from the comment object since thats a defininte, and its related to the current comment being viewed.

There is no reason why $post should not be set correctly (If it isnt, Then other functions will be failing too).

comment:5 @juggledad6 years ago

The doc for this routine states '@param int $post Optional. Post that the comment is going to be displayed on.' There fore I should be able to call this routine without $post. If that is true, then you need to get the post ID from the comment. This is eithor a bug in this code or the documentation needs to be changed to make $post a REQUIRED parameter instead of an OPTIONAL.

The Atahualpa Theme ends up calling this routine when adding comments to PAGES (an option the theme allows) and the $post is not being passed thus causing an error.

Changing the code so the the routine gets the post ID from the comment takes care of both cases, weither you pass teh $post or not and gaurentees that the correct post is associated with the comment.

Therotically, I could get a comment for post 'A', then get post 'B' and then call this routine. With the code the way it is, the comment reply would be tied to post 'B' and not post 'A' as it should be. With the change, the comment reply would be associated with the correct post ie 'A'.

granted this is a made up case, but it issustrates why the routine needs to get the post ID from the comment_post_ID

comment:6 @dd326 years ago

The Atahualpa Theme ends up calling this routine when adding comments to PAGES (an option the theme allows) and the $post is not being passed thus causing an error.

There is no reason why $post should not be set for pages, It refers to the currrently-displayed "post" A post being an object in the database regardless of its post_type.

comment:7 @dd326 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [13396]) Prefer $comment::comment_post_ID over $post global. Fixes #12217

Note: See TracTickets for help on using tickets.