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
Changes from all commits
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 |
---|---|---|
|
@@ -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} | ||
|
@@ -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 ); | ||
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. I don't think we need a filter here. 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. 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
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. @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 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. 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; | ||
} | ||
|
There was a problem hiding this comment.
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.