#56243 closed task (blessed) (fixed)
Use a consistent parameter name for functions accepting a post ID or post object
Reported by: | SergeyBiryukov | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Posts, Post Types | Keywords: | has-patch |
Focuses: | docs, coding-standards | Cc: |
Description
Most functions that accept a post ID or post object use $post
as the parameter name.
- Some functions use
$post_id
or just$id
, which should be renamed to$post
. - Some functions can accept a
WP_Post
object, but it's not explicitly documented:get_the_category()
get_the_category_list()
the_category()
get_the_tag_list()
get_the_term_list()
the_terms()
This ticket aims to bring some consistency.
Change History (13)
#3
follow-up:
↓ 4
@
2 years ago
There are also a lot of occurrences of $post_ID
(some of which need to be an ID and not a post object, so not sure if they all should be changed to $post
).
I can provide a list of those occurrences if needed...but I suspect @SergeyBiryukov is better at searching the code base than I am :-)
Not sure if it would be "scope creep" for this ticket, there are also mix of $comment_ID
and $comment_id
throughout code. Should those be standardized as well?
#4
in reply to:
↑ 3
;
follow-up:
↓ 5
@
2 years ago
Replying to pbiron:
There are also a lot of occurrences of
$post_ID
(some of which need to be an ID and not a post object, so not sure if they all should be changed to$post
).
I think those that do not accept a WP_Post
object should be renamed to $post_id
for consistency.
I can provide a list of those occurrences if needed...
That would be helpful, thanks!
Not sure if it would be "scope creep" for this ticket, there are also mix of
$comment_ID
and$comment_id
throughout code. Should those be standardized as well?
Indeed, I've started looking into that too and will open a similar ticket for those :) It's worth noting that $comment_ID
causes a WPCS warning and should be fixed anyway:
| WARNING | [ ] Variable "$comment_ID" is not in valid snake_case | | format, try "$comment_i_d" | | (WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase)
#5
in reply to:
↑ 4
;
follow-up:
↓ 6
@
2 years ago
Replying to SergeyBiryukov:
Replying to pbiron:
I can provide a list of those occurrences if needed...
That would be helpful, thanks!
Will do that in the next few days.
Indeed, I've started looking into that too and will open a similar ticket for those :) It's worth noting that
$comment_ID
causes a WPCS warning and should be fixed anyway:
| WARNING | [ ] Variable "$comment_ID" is not in valid snake_case | | format, try "$comment_i_d" | | (WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase)
The WPCS errors are what brought those to my attention. In plugins I write, when I hook into actions/filters, I try to name the parameters to my callbacks exactly the same as in the core hook DocBlock...and I often have to add // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
to silences those warnings (and I've been working on one such plugin that deals with comments lately :-).
I'll keep an eye out for that other ticket being opened. thanx!
#7
@
2 years ago
The change to comment_form()
appears to change the behaviour.
Previously if an invalid post ID was passed to the function comments_open()
would return false and no comment form be displayed.
The new conditional $post_id = $post ? $post->ID : get_the_ID();
means that if an invalid post ID is passed to the comment_form()
, the form will be display for the current global post.
This ticket was mentioned in PR #2988 on WordPress/wordpress-develop by peterwilsoncc.
2 years ago
#8
- Keywords has-patch added
See https://core.trac.wordpress.org/ticket/56243#comment:7
This restores how comment_form()
behaves if an invalid post ID is passed to the function.
I tested by making HTTP requests with the following mini plugin.
{{{php
<?php
/
- Comment form test. */
return;
add_action(
'wp_head',
function() {
$testing = 1; also try null, 50000000
comment_form( [], $testing );
exit;
},
5
);
add_action(
'comment_form_comments_closed',
function() {
var_dump( 'doing comment_form_comments_closed' );
},
5
);
}}}
#9
@
2 years ago
The linked pull request:
- restores the behaviour of
comment_form()
- updates the description of the
comment_form_comments_closed
action to note it fires with an invalid post ID - includes some tests to determine the form displays as expected.
This ticket was mentioned in Slack in #core by sergey. View the logs.
2 years ago
#11
@
2 years ago
- Type changed from enhancement to task (blessed)
Converting this to a task, as these are mostly coding standards fixes, and there are some follow-up changes to make.
SergeyBiryukov commented on PR #2988:
2 years ago
#13
Thanks for the PR! Merged in r54488.
In 53715: