Skip to content
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

SQL_CALC_FOUND_ROWS statements refactored #1203

Merged
merged 1 commit into from Jan 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions classes/class-query.php
Expand Up @@ -229,7 +229,7 @@ public function query( $args ) {
/**
* BUILD THE FINAL QUERY
*/
$query = "SELECT SQL_CALC_FOUND_ROWS {$select}
$query = "SELECT {$select}
FROM $wpdb->stream
{$join}
WHERE 1=1 {$where}
Expand All @@ -246,12 +246,29 @@ public function query( $args ) {
*/
$query = apply_filters( 'wp_stream_db_query', $query, $args );

$result = array();
// Build result count query.
$count_query = "SELECT COUNT(*) as found
FROM $wpdb->stream
{$join}
WHERE 1=1 {$where}";

/**
* Filter allows the result count query to be modified before execution.
*
* @param string $query
* @param array $args
*
* @return string
*/
$count_query = apply_filters( 'wp_stream_db_count_query', $count_query, $args );
Comment on lines +249 to +263
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivankruchkoff Here's result count query.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a filter here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it will be needed, If someone is modifying the select query, they would need to modify the count query as well to make the count match the results shown.


/**
* QUERY THE DATABASE FOR RESULTS
*/
$result['items'] = $wpdb->get_results( $query ); // @codingStandardsIgnoreLine $query already prepared
$result['count'] = $result['items'] ? absint( $wpdb->get_var( 'SELECT FOUND_ROWS()' ) ) : 0;
$result = array(
'items' => $wpdb->get_results( $query ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
'count' => absint( $wpdb->get_var( $count_query ) ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
Comment on lines +269 to +270
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivankruchkoff @kasparsd @derekherman I added explicit ignore comments here. I think don't anymore sanitation needs to be done because all dynamic parts of the query are run thru $wpdb->prepare() earlier in the function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, all the dynamic parts seem to be fine. However, we're now passing those queries through filters as the very last thing. I guess we need to trust that whoever is using that filter will do their job to sanitize the adjusted parts.

);

return $result;
}
Expand Down