Skip to content

Commit

Permalink
Comment query must return int if 'count' var is set
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed May 25, 2023
1 parent 4b6f82d commit deaa421
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
<issueHandlers>
<MixedAssignment errorLevel="suppress"/>
<UnresolvableInclude errorLevel="suppress"/>
<RedundantConditionGivenDocblockType errorLevel="suppress"/>
</issueHandlers>
</psalm>
20 changes: 19 additions & 1 deletion src/CommentsDisabler.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public function init(): void
$addFilter('xmlrpc_methods', 'replaceXmlrpcMethods');
$addFilter('rewrite_rules_array', 'filterRewriteRules');
$addFilter('wp_count_comments', 'filterCountComments');
$addFilter('comments_pre_query', 'shortCircuitCommentQuery', 2);
$addFilter('allowed_block_types_all', 'disableBlocks');

add_filter('comments_open', '__return_false', PHP_INT_MAX);
add_filter('pings_open', '__return_false', PHP_INT_MAX);
add_filter('comments_pre_query', '__return_empty_array', PHP_INT_MAX);
add_filter('feed_links_show_comments_feed', '__return_false', PHP_INT_MAX);
add_filter('feed_links_extra_show_post_comments_feed', '__return_false', PHP_INT_MAX);
add_filter('post_comments_feed_link', '__return_empty_string', PHP_INT_MAX);
Expand Down Expand Up @@ -572,6 +572,24 @@ private function filterCountComments(): object
];
}

/**
* @param mixed $commentData
* @param mixed $commentQuery
* @return mixed
*/
public function shortCircuitCommentQuery(mixed $commentData, mixed $commentQuery): mixed
{
if (!($commentQuery instanceof \WP_Comment_Query)) {
return $commentData;
}
$queryVars = $commentQuery->query_vars;
if (is_array($queryVars) && !empty($queryVars['count'])) {
return 0;
}

return [];
}

/**
* @param mixed $enabled
* @return bool|array
Expand Down

0 comments on commit deaa421

Please sign in to comment.