Repository navigation
Comment REST API route: add support for filtering by multiple statuses. #9870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
82c76fa
1fc983f
4ee6fe8
bdd5a6d
b878d8b
f435d93
7b22c39
6225ae6
f121b3e
5fba5f6
1ee7a86
3bf3121
f210ce5
faca069
0426084
c4159ea
a2972fd
4b93f1a
d064fb8
5217fd2
cd4496f
c1c7823
f44de34
b9b22de
9a34a07
f29b134
6dd63d2
79d5aa4
303f021
325b6e7
f18c8e4
60db941
dffa629
72be073
2da2f56
dea4676
f66dabe
a309cef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -185,7 +185,7 @@ public function get_items_permissions_check( $request ) { | |
|
|
||
| foreach ( $protected_params as $param ) { | ||
| if ( 'status' === $param ) { | ||
| if ( 'approve' !== $request[ $param ] ) { | ||
| if ( array( 'approve' ) !== $request[ $param ] ) { | ||
| $forbidden_params[] = $param; | ||
| } | ||
| } elseif ( 'type' === $param ) { | ||
|
|
@@ -228,7 +228,7 @@ public function get_items_permissions_check( $request ) { | |
| if ( ! current_user_can( 'edit_posts' ) ) { | ||
| foreach ( $protected_params as $param ) { | ||
| if ( 'status' === $param ) { | ||
| if ( 'approve' !== $request[ $param ] ) { | ||
| if ( array( 'approve' ) !== $request[ $param ] ) { | ||
| $forbidden_params[] = $param; | ||
| } | ||
| } elseif ( 'type' === $param ) { | ||
|
|
@@ -2175,10 +2175,13 @@ public function get_collection_params() { | |
| ); | ||
|
|
||
| $query_params['status'] = array( | ||
| 'default' => 'approve', | ||
| 'description' => __( 'Limit result set to comments assigned a specific status. Requires authorization.' ), | ||
| 'sanitize_callback' => 'sanitize_key', | ||
| 'type' => 'string', | ||
| 'default' => array( 'approve' ), | ||
| 'description' => __( 'Limit result set to comments assigned one or more statuses. Requires authorization.' ), | ||
| 'type' => 'array', | ||
| 'items' => array( | ||
| 'type' => 'string', | ||
| ), | ||
| 'sanitize_callback' => array( $this, 'sanitize_comment_statuses' ), | ||
| 'validate_callback' => 'rest_validate_request_arg', | ||
|
adamsilverstein marked this conversation as resolved.
|
||
| ); | ||
|
|
||
|
|
@@ -2516,6 +2519,29 @@ protected function check_is_comment_content_allowed( $prepared_comment ) { | |
| return '' !== $check['comment_content']; | ||
| } | ||
|
|
||
| /** | ||
| * Sanitizes a single comment status or a list of comment statuses. | ||
| * | ||
| * @since 7.2.0 | ||
| * | ||
| * @param string[]|string $statuses Comment status or array of comment statuses. | ||
| * @return string[] Sanitized array of comment statuses. | ||
| * @phpstan-return list<non-empty-lowercase-string> | ||
| */ | ||
| public function sanitize_comment_statuses( $statuses ): array { | ||
| $statuses = array_unique( array_map( 'sanitize_key', wp_parse_list( $statuses ) ) ); | ||
|
|
||
| // Only drop empty values. A literal '0' is a queryable status, as comments are held with `comment_approved` of '0'. | ||
| $statuses = array_filter( | ||
| $statuses, | ||
| static function ( $status ) { | ||
| return '' !== $status; | ||
| } | ||
| ); | ||
|
|
||
| return array_values( $statuses ); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some late follow-up here is that we’re performing a lot of needless allocation here, and while the performance impact of this function is likely not relevant in the overall picture, it’s still part of painting that. effectively this could be replaced by one big iteration, rather than the current five passes with intermediate arrays. here is an alternative idea skipping that allocation. if ( is_string( $statuses ) ) {
$statuses = wp_parse_list( $statuses );
}
$sanitized = array();
$seen = array();
foreach ( $statuses as $status ) {
if ( ! is_scalar( $status ) ) {
continue;
}
$key = sanitize_key( $status );
if ( '' === $key || isset( $seen[ $key ] ) ) {
continue;
}
$seen[ $key ] = true;
$sanitized[] = $key;
}
return $sanitized;
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback @dmsnell, I'll open a follow up PR to optimize this.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. And since the number of statuses is going to be very very small, then a hash lookup for if ( is_string( $statuses ) ) {
$statuses = wp_parse_list( $statuses );
}
$sanitized = array();
foreach ( $statuses as $status ) {
if ( ! is_scalar( $status ) ) {
continue;
}
$key = sanitize_key( $status );
if ( '' !== $key && ! in_array( $key, $sanitized, true ) ) {
$sanitized[] = $key;
}
}
return $sanitized;
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: widen the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
it depends if we can guarantee that @westonruter. I have been using this pattern too, but if the list of statuses is user-provided, then there are catastrophic cases that lead to DoS so if we assume the list is small, we should make sure before we introduce quadtratic lookup. |
||
|
|
||
| /** | ||
| * Checks that a post can receive a comment or a note from the current user. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.