Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2531,17 +2531,29 @@ protected function check_is_comment_content_allowed( $prepared_comment ) {
* @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 ) ) );
if ( ! is_array( $statuses ) ) {
$statuses = 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;
$sanitized = array();
$seen = array();
foreach ( $statuses as $status ) {
if ( ! is_scalar( $status ) ) {
continue;
}
);

return array_values( $statuses );
$key = sanitize_key( $status );

// Only skip empty values. A literal '0' is a queryable status, as comments are held with `comment_approved` of '0'.
if ( '' === $key || isset( $seen[ $key ] ) ) {
continue;
}

$seen[ $key ] = true;
$sanitized[] = $key;
}
Comment thread
westonruter marked this conversation as resolved.

return $sanitized;
}

/**
Expand Down
21 changes: 12 additions & 9 deletions tests/phpunit/tests/rest-api/rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,18 @@ public function test_sanitize_comment_statuses( $statuses, array $expected ) {
*/
public function data_sanitize_comment_statuses(): array {
return array(
'a single status' => array( 'approve', array( 'approve' ) ),
'a comma-separated list' => array( 'approve,hold', array( 'approve', 'hold' ) ),
'an array of statuses' => array( array( 'approve', 'hold' ), array( 'approve', 'hold' ) ),
'mixed case statuses' => array( 'APPROVE,Hold', array( 'approve', 'hold' ) ),
'duplicate statuses' => array( array( 'approve', 'approve', 'hold' ), array( 'approve', 'hold' ) ),
'invalid characters' => array( 'howdy&nbsp;admin', array( 'howdynbspadmin' ) ),
'an empty string' => array( '', array() ),
'a literal 0 status' => array( '0', array( '0' ) ),
'0 mixed with a status' => array( '0,approve', array( '0', 'approve' ) ),
'a single status' => array( 'approve', array( 'approve' ) ),
'a comma-separated list' => array( 'approve,hold', array( 'approve', 'hold' ) ),
'an array of statuses' => array( array( 'approve', 'hold' ), array( 'approve', 'hold' ) ),
'mixed case statuses' => array( 'APPROVE,Hold', array( 'approve', 'hold' ) ),
'duplicate statuses' => array( array( 'approve', 'approve', 'hold' ), array( 'approve', 'hold' ) ),
'invalid characters' => array( 'howdy&nbsp;admin', array( 'howdynbspadmin' ) ),
'an empty string' => array( '', array() ),
'a literal 0 status' => array( '0', array( '0' ) ),
'0 mixed with a status' => array( '0,approve', array( '0', 'approve' ) ),
'duplicates after sanitizing' => array( array( 'Approve', 'approve', 'APPROVE' ), array( 'approve' ) ),
'empty values in an array' => array( array( '', 'approve', '&', 'hold' ), array( 'approve', 'hold' ) ),
'non-scalar values' => array( array( array( 'approve' ), 'hold', null ), array( 'hold' ) ),
);
}

Expand Down
Loading